如果未达到某个类别的最低数量,则阻止 WooCommerce 结帐,除非添加另一个类别
我正在购物车中进行检查,以应用一条规则:如果添加冷藏类别中的商品,则至少需要 3 件冷藏类别商品才能结账。 - 这有效。
但是,如果还添加了捆绑类别中的商品,则不应强制执行上述冷藏规则。
例如,至少需要 3 件冷藏类别商品,除非购物车中有捆绑商品,在这种情况下,请忽略冷藏规则。
我至少有 3 个冷冻规则有效,但如果检测到捆绑类别中的项目,我无法获取排除此规则的代码?
基于如果未达到特定类别的最低数量,则阻止 WooCommerce 结帐答案代码,这是我的尝试:
function action_woocommerce_check_cart_items() {
// Only run on the cart or checkout pages
if ( is_cart() || is_checkout() ) {
// Minimum
$minimum = 3;
// Category
$category = 'chilled';
$category2 = 'bundles';
// Initialize
$total = 0;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Product id
$product_id = $cart_item['product_id'];
// Has certain category
if ( has_term( $category, 'product_cat', $product_id ) ) {
// Add to total
$total += $cart_item['quantity'];
}elseif (has_term ($category2, 'product_cat', $product_id)) {
break;
}
}
// When total is greater than 0 but less than the minimum
if ( $total > 0 && $total < $minimum ) {
// Notice
wc_add_notice( sprintf( __( '<strong>A minimum of %s products are required from the CHILLED category before checking out.</strong>', 'woocommerce' ), $minimum ), 'error' );
// Optional: remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
I am doing a check in the cart to apply a rule that if a item from the chilled category is added, a minimum of 3 chilled category items are required to check out. - This works.
However, if an item from the bundles category is also added, then the above chilled rule should not be enforced.
e.g. Minimum of 3 chilled category items required, unless a bundle item is in the cart, in which case, disregard the chilled rule.
I have the minimum 3 chilled rule working, but I can't get the code to exclude this rule if a item from the bundles category is detected?
Based on Prevent WooCommerce checkout if minimum quantity for a specific category is not reached answer code, this is my attempt:
function action_woocommerce_check_cart_items() {
// Only run on the cart or checkout pages
if ( is_cart() || is_checkout() ) {
// Minimum
$minimum = 3;
// Category
$category = 'chilled';
$category2 = 'bundles';
// Initialize
$total = 0;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Product id
$product_id = $cart_item['product_id'];
// Has certain category
if ( has_term( $category, 'product_cat', $product_id ) ) {
// Add to total
$total += $cart_item['quantity'];
}elseif (has_term ($category2, 'product_cat', $product_id)) {
break;
}
}
// When total is greater than 0 but less than the minimum
if ( $total > 0 && $total < $minimum ) {
// Notice
wc_add_notice( sprintf( __( '<strong>A minimum of %s products are required from the CHILLED category before checking out.</strong>', 'woocommerce' ), $minimum ), 'error' );
// Optional: remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在当前循环中结束执行还不够,还需要在 if 条件中添加额外的规则。
所以你得到:
End the execution in the current loop will not suffice, you also need to add an extra rule to the if condition.
So you get: