Filter to check quantity at checkout

I have a specific function I would like to implement with WooCommerce if possible. Is there a way that I can have WooCommerce check if the quantity of items in the cart is divisible by 12?

Basically I have a shop setup but I need customers to order in quantities of 12 (that’s the size of the boxes I ship the items in). However, I have a few product variations (size, width, etc.) and I want the customers to be able to mix and match variations in order to reach a quantity of 12 (or 24, 36, etc.)

Read More

I’m gathering that the easiest way to do this is probably to install a function that checks the total cart quantity and if it is divisible by 12. If so, they can proceed. If not, they need to change their quantity to continue. Does anyone have experience on if there is a plugin that suits this need or if there is some custom code I can add?

Any help is greatly appreciated.

Regards,
Ryan

Related posts

Leave a Reply

1 comment

  1. This should work for you:

    Add the following to the functions.php file, at checkout it will check and see if the quantity is a multiple of 12, if it is then it’s fine, if not you get the error message You need to buy in quantities of 12 products and won’t be able to proceed.

    add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
    function woocommerce_check_cart_quantities() 
    {
        global $woocommerce;
        $multiples = 12;
        $total_products = 0;
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) 
        {
            $total_products += $values['quantity'];
        }
        if ( ( $total_products % $multiples ) > 0 )
            $woocommerce->add_error( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
    }
    

    The above is slightly modified from this post.