如果未达到类别的最低数量,除非添加另一个类别,否则阻止 WooCommerce 结帐

Prevent WooCommerce checkout if minimum quantity for a category is not reached unless another category is added

我正在检查购物车以应用规则,如果添加了 冷藏 类别的商品,则至少需要 3 件冷藏类别的商品才能结帐. - 这行得通。

但是,如果还添加了 捆绑包 类别中的项目,则不应执行上述冷藏规则。

例如至少需要 3 件冷藏类别商品,除非购物车中有捆绑商品,在这种情况下,请忽略冷藏规则。

我的最小 3 冷藏规则有效,但如果检测到捆绑包类别中的项目,我无法获得排除此规则的代码?

基于答案代码,这是我的尝试:

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

在当前循环中结束执行还不够,还需要在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 );