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

Woocommerce exclude specific cart category from minimum order requirement

我的最低订购量为 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' );

        }
    }
}