Created
May 22, 2018 23:38
-
-
Save vtsara/f45550595bf1416ce6563f2d8ea8b143 to your computer and use it in GitHub Desktop.
simple max quantity for certain categories | woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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