How to limit Purchase Quantity of products of a category in Woocommerce?

By default a customer can purchase any quantity from a Woocommerce store. Sometimes store owners need to sell products which can only be sold in specific quantities.

In this tutorial, We will learn how to limit Quantity of products of a certain category to be sold.

So, to do this add the following lines of code at the end of your theme’s functions.php file:

add_action( 'woocommerce_check_cart_items', 'check_total' );
function check_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        global $woocommerce, $product;

        $total_quantity = 0;
        $display_notice = 1;
        $i = 0;
        //loop through all cart products
        foreach ( $woocommerce->cart->cart_contents as $product ) {

            // See if any product is from the cuvees category or not
            if ( has_term( 'category-1', 'product_cat', $product['product_id'] )) {
                $total_quantity += $product['quantity'];
            }

        }
        // Set up the acceptable totals and loop through them so we don't have an ugly if statement below.
        $acceptable_totals = array(1, 2, 3, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 72, 96, 120);

        foreach($acceptable_totals as $total_check) {
            if ( $total_check == $total_quantity ) { $display_notice = 0; } 
        }

        foreach ( $woocommerce->cart->cart_contents as $product ) {
            if ( has_term( 'category-1', 'product_cat', $product['product_id'] ) ) {
                if( $display_notice == 1 && $i == 0 ) {
                    // Display our error message
                    wc_add_notice( sprintf( 'This product can only be sold in following quantities 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120.</p>', $total_quantity),
                    'error' );
                }
                $i++;
            }
        }
    }

 Now let me explain where you need to make changes in the code.

In the code you’ll see following line two times.

if ( has_term( 'category-1', 'product_cat', $product['product_id'] )) {

In this line you need to replace ‘category-1’ with your category slug. This line checks whether the product is in given category.

Now, We need to define or change allowed quantities. For that you can make changes to the following line.

$acceptable_totals = array(1, 2, 3, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 72, 96, 120);

Here in the array enter allowed quantities.

To change the error message being displayed you need to edit this line:

wc_add_notice( sprintf( '<p>This product can only be sold in following quantities 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120.</p>', $total_quantity),
   'error' );

Just edit the text inside <p> tags.

Edit code according to your needs and save the file.

Now add the product of that category by clicking on add to cart button, When you will try to proceed with invalid quantity or even try to update the cart it will show you the error message and won’t allow you to proceed.

2015-08-28_1544

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

2 thoughts on “How to limit Purchase Quantity of products of a category in Woocommerce?”

  1. hello .. very good this hint / help!
    Tell me, how could I use this logic to limit by brand and not by category? Could it be that?

    congratulations content

Leave a Comment

Your email address will not be published. Required fields are marked *