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

时间:2018-11-20 00:43:48

标签: php wordpress woocommerce shopping-cart hook-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' );

        }
    }
}


}

}

1 个答案:

答案 0 :(得分: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' );

        }
    }
}