Woocommerce将特定的购物车类别从最低订单要求中排除



我的最低订单量是4件或8件。例如,1、2、3、5、6、7项为无效数量。

我正在尝试将类别为圣诞节的产品从该规则中排除。

例如,客户可以从圣诞节类别中购买1件商品,但必须从所有其他类别中至少购买4或8件商品。

下面的代码可以自行检查购物车中的物品是否来自圣诞节类别:

add_action('woocommerce_before_cart', 'bbloomer_check_category_in_cart');
function bbloomer_check_category_in_cart() {
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// If Cart has category "christmas", set $cat_in_cart to true
if ( has_term( 'christmas', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}

基于上述条件,我的代码的第二部分不起作用。然而,它确实是独立运作的

// If no christmas category in cart, run minimum order code:      
if ( !$cat_in_cart ) {

add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum number of products before checking out     
$minimum_num_products = 8;
$minimum_taster_products = 4;
// Get the Cart's total number of products
$cart_num_products = WC()->cart->cart_contents_count;
// Compare values and add an error is Cart's total number of products
// happens to be less than the minimum required before checking out.
// A Minimum of 4 OR 8 products is required before checking out. (Cont. below)
if( ($cart_num_products < $minimum_num_products) && ($cart_num_products > $minimum_taster_products) ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Our Smallest Plan requires at least %s snacks per order.</strong>' 
. '<br />Current number of snacks: %s.',
$minimum_num_products,
$cart_num_products ),
'error' );
} else if ($cart_num_products < $minimum_taster_products) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s snacks is required for a TASTER BOX.</strong>' 
. '<br />Current number of snacks: %s.',
$minimum_taster_products,
$cart_num_products ),
'error' );
}
}
}

}
}

解决了我自己。而不是两个单独的函数,需要转移到一个函数中并使用woocommerce_check_cart_items检查类别

add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// If Cart has category "download", set $cat_in_cart to true
if ( has_term( 'christmas', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}

// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum number of products before checking out     
$minimum_num_products = 8;
$minimum_taster_products = 4;
// Get the Cart's total number of products
$cart_num_products = WC()->cart->cart_contents_count;
// Compare values and add an error is Cart's total number of products
// happens to be less than the minimum required before checking out.
// A Minimum of 4 OR 8 products is required before checking out. (Cont. below)
if( ( !$cat_in_cart ) && ($cart_num_products < $minimum_num_products) && ($cart_num_products > $minimum_taster_products) ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Our Smallest Plan requires at least %s snacks per order.</strong>' 
. '<br />Current number of snacks: %s.',
$minimum_num_products,
$cart_num_products ),
'error' );
} else if ( ( !$cat_in_cart ) && ($cart_num_products < $minimum_taster_products) ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s snacks is required for a TASTER BOX.</strong>' 
. '<br />Current number of snacks: %s.',
$minimum_taster_products,
$cart_num_products ),
'error' );
}
}
}

最新更新