Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am doing a check in the cart to apply a rule that if a item from the chilled category is added, a minimum of 3 chilled category items are required to check out. – This works.

However, if an item from the bundles category is also added, then the above chilled rule should not be enforced.

e.g. Minimum of 3 chilled category items required, unless a bundle item is in the cart, in which case, disregard the chilled rule.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have the minimum 3 chilled rule working, but I can’t get the code to exclude this rule if a item from the bundles category is detected?

Based on Prevent WooCommerce checkout if minimum quantity for a specific category is not reached answer code, this is my attempt:

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

>Solution :

End the execution in the current loop will not suffice, you also need to add an extra rule to the if condition.

So you get:

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 );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading