3

In WooCommerce, I am trying to set a minimum quantity for cart items from a specific product category.

Based on "Minimum cart item quantity for a specific product category in WooCommerce", here is my code attempt :

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $category      = 'games'; // The targeted product category
    $min_item_qty  = 4; // Minimum Qty required (for each item)
    $display_error = false; // Initializing

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        $item_quantity = $cart_item['quantity']; // Cart item quantity
        $product_id    = $cart_item['product_id']; // The product ID

        // For cart items remaining to "Noten" producct category
        if( has_term( $category, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
            wc_clear_notices(); // Clear all other notices

            // Add an error notice (and avoid checkout).
            wc_add_notice( sprintf( 'You should at least pick', $min_item_qty ,'products  for'  ,$category,  'category' ), 'error' );
            break; // Stop the loop
        }
    }
}

It doesn't work as I would like as the a minimum quantity is set for the first cart item from the specific product category, but not globally for all items from that specific product category. Any help is appreciated.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
yosober
  • 345
  • 3
  • 16

1 Answers1

5

You need first to count the number of items from the targeted product category… then you can display the error notice when the number of items are below the minimum defined:

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
    $category  = 'Games'; // The targeted product category
    $min_qty   = 4; // Minimum Qty required (for each item)
    $qty_count = 0; // Initializing

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $item ) {
        // Count items from the targeted product category
        if( has_term( $category, 'product_cat', $item['product_id'] ) ) {
            $qty_count += $item['quantity'];
        }
    }

    // Display error notice avoiding checkout
    if( $qty_count != 0 && $qty_count < $min_qty ) {
        wc_clear_notices(); // Clear all other notices

        // Add an error notice (and avoid checkout).
        wc_add_notice( sprintf(
            __("You should pick at least %s items from %s category.", "woocommerce"),
            '<strong>' . $min_qty . '</strong>',
            '<strong>' . $category . '</strong>'
        ), 'error' );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • sir this code have problem because if user had not any products from specific category this code will run anyway for example i want that if user had not any products from specific category in his cart The code should not be executed – yosober Sep 04 '19 at 05:09
  • 1
    @yosober I have updated the code… it should work now as you expect. – LoicTheAztec Sep 04 '19 at 11:54
  • I have implement the code it's working on a single category. What if I want to use in multiple category. For category Grocery it require 10 units, Fruits 5 unites and so on. – Abid Oct 24 '21 at 10:47
  • Only works for simple products not product variations. – Dev Mar 02 '23 at 11:49