我正在購物車中進行檢查以應用一條規則,即如果添加了冷藏類別的 商品,則至少需要 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 );
uj5u.com熱心網友回復:
在當前回圈中結束執行是不夠的,您還需要在 if 條件中添加額外的規則。
所以你得到:
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';
$category_2 = 'bundles';
// Initialize
$total = 0;
$flag = true;
// 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'];
// Has other category
} elseif ( has_term( $category_2, 'product_cat', $product_id ) ) {
// Break loop
$flag = false;
break;
}
}
// When total is greater than 0 but less than the minimum & flag is still true
if ( ( $total > 0 && $total < $minimum ) && $flag ) {
// Notice
wc_add_notice( sprintf( __( 'A minimum of %s products are required from the %s category before checking out.', 'woocommerce' ), $minimum, $category ), '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 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/433814.html
標籤:WordPress woocommerce 产品 大车 退房
上一篇:頁面之間的wordpress分頁
