Set min quantity for some product categories or product IDs in Woocommerce 3.3 Set min quantity for some product categories or product IDs in Woocommerce 3.3 wordpress wordpress

Set min quantity for some product categories or product IDs in Woocommerce 3.3


Update 2 (It works too now for archive pages and on cart for product variations).

The following code will set a minimal quantity for specific product categories:

// On single product pagesadd_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );function min_qty_input_args( $args, $product ) {    ## ---- Your settings ---- ##    $product_categories = array('Burberry', 'Daniel Wellington',        'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');    $quantity = 2;    ## ---- The code: set minimun quantity for specific product categories ---- ##    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();    if( has_term( $product_categories, 'product_cat', $product_id ) ){        $args['min_value'] = $quantity;    }    return $args;}// On archives pagesadd_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );function min_qty_loop_add_to_cart_button( $button, $product  ) {    // Only for non variable products    if( $product->is_type( 'variable' ) ) return $button; // Exit    ## ---- Your settings ---- ##    $product_categories = array('Burberry', 'Daniel Wellington',        'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');    $quantity = 2;    ## ---- The code: set minimun quantity for specific product categories ---- ##    if( has_term( $product_categories, 'product_cat', $product->get_id() ) ){        $class = implode( ' ', array_filter( array(            'button',            'product_type_' . $product->get_type(),            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',            $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',        ) ) );        $button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',            esc_url( $product->add_to_cart_url() ),            esc_attr( isset( $quantity ) ? $quantity : 1 ),            esc_attr( $product->get_id() ),            esc_attr( $product->get_sku() ),            esc_attr( isset( $class ) ? $class : 'button' ),            esc_html( $product->add_to_cart_text() )        );    }    return $button;}

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

For product tags, you will have just to change 'product_cat' by 'product_tag' in the code.


ADDITION: For product IDS (array of IDs)

To make it work for product Ids instead use:

// On single product pagesadd_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );function min_qty_input_args( $args, $product ) {    ## ---- Your settings ---- ##    $product_ids = array('23', '52', '75', '87', '90', '102');    $quantity = 2;    ## ---- The code: set minimun quantity for specific product Ids ---- ##    if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){        $args['min_value'] = $quantity;    }    return $args;}// On archives pagesadd_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );function min_qty_loop_add_to_cart_button( $button, $product  ) {    // Only for non variable products    if( $product->is_type( 'variable' ) ) return $button; // Exit    ## ---- Your settings ---- ##    $product_ids = array('23', '52', '75', '87', '90', '102');    $quantity = 2;    ## ---- The code: set minimun quantity for specific product IDs ---- ##    if( in_array( $product->get_id(), $product_ids ) ){        $class = implode( ' ', array_filter( array(            'button',            'product_type_' . $product->get_type(),            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',            $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',        ) ) );        $button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',            esc_url( $product->add_to_cart_url() ),            esc_attr( isset( $quantity ) ? $quantity : 1 ),            esc_attr( $product->get_id() ),            esc_attr( $product->get_sku() ),            esc_attr( isset( $class ) ? $class : 'button' ),            esc_html( $product->add_to_cart_text() )        );    }    return $button;}

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