minimum order item for certain products exempting 2 other products (in bulk)

I have a client that sells books and all their books (except for 2 products, the show special) can be purchased in assorted lots of 12 only.
I have gotten a script that allows me state that 12 lots is required but I need to exclude the 2 specials products from the rule.

Here is the script

// Set a minimum number of products requirement before checking out
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 = 12;
        // 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.
        // Will display a message along the lines of
        // A Minimum of 20 products is required before checking out. (Cont. below)
        // Current number of items in the cart: 6   
        if( $cart_num_products < $minimum_num_products ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' 
                . '<br />Current number of items in the cart: %s.',
                $minimum_num_products,
                $cart_num_products ),
            'error' );
        }
    }
}

I will be most grateful to have this sorted by able developers here.

Thank you in advance.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this one (replace $special_products = array( <id1>, <id2>, ... ); with your list of product IDs):

// Set a minimum number of products requirement before checking out
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() ) {

        // List of "special" products IDs
        $special_products = array( <id1>, <id2>, ... );

        // Get an array of product IDs
        $products = array_column( WC()->cart->get_cart(), 'product_id' );
        if ( $products ) {
            foreach ( $products as $product ) {
                // Return immediately if a "special" product ID found
                // (therefore skipping product count check)
                if ( in_array( $product, $special_products ) ) return;
            }
        }

        // Set the minimum number of products before checking out
        $minimum_num_products = 12;
        // Get the Cart's total number of products
        $cart_num_products = WC()->cart->get_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.
        // Will display a message along the lines of
        // A Minimum of 20 products is required before checking out. (Cont. below)
        // Current number of items in the cart: 6   
        if( $cart_num_products < $minimum_num_products ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' 
                . '<br />Current number of items in the cart: %s.',
                $minimum_num_products,
                $cart_num_products ),
            'error' );
        }
    }
}

I replace $cart_num_products = WC()->cart->cart_contents_count; with $cart_num_products = WC()->cart->get_cart_contents_count(); because the first form is deprecated since WooCommerce 3.0. global $woocommerce; is also not needed anymore, nowadays any access to global $woocommerce object should be done via WC() function.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x