Skip to content

Instantly share code, notes, and snippets.

@vtsara
Created May 22, 2018 23:38
Show Gist options
  • Save vtsara/f45550595bf1416ce6563f2d8ea8b143 to your computer and use it in GitHub Desktop.
Save vtsara/f45550595bf1416ce6563f2d8ea8b143 to your computer and use it in GitHub Desktop.
simple max quantity for certain categories | woocommerce
<?php
// add maximum quantity for cafe pickup items
add_filter( 'woocommerce_quantity_input_args', 'lg_woocommerce_quantity_input_args', 10, 2 );
function lg_woocommerce_quantity_input_args( $args, $product ) {
if ( has_term( 'pastries', 'product_cat' ) || has_term( 'case', 'product_cat' ) || has_term( 'lunch', 'product_cat' ) ) {
$args['input_value'] = 1; // Starting value (we only want to affect product pages, not cart)
$args['max_value'] = 10; // Maximum value (we want people to use catering to buy more than 10)
if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
$stock = $product->get_stock_quantity();
// Limit our max by the available stock, if stock is lower
// Set to lessor of stock qty or max allowed
$args['max_value'] = min( $stock, $args['max_value'] );
}
}
return $args;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment