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

问题描述 投票:-1回答:1

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

我试图排除以下类别的产品:来自此规则的圣诞节。

例如因此,顾客可以从圣诞类别购买1件商品,但必须购买至少4件或8件商品来自所有其他类别。

Code Code自行检查以查看购物车中的商品是否来自圣诞节类别:

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' );

        }
    }
}


}

}
php wordpress woocommerce shopping-cart hook-woocommerce
1个回答
0
投票

解决了自己。需要移动到单个函数并使用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' );

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.