Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Last active November 17, 2018 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikparmar/73078280f966757dfd5410fa947d0f78 to your computer and use it in GitHub Desktop.
Save kartikparmar/73078280f966757dfd5410fa947d0f78 to your computer and use it in GitHub Desktop.
Setting minimum and maximum for quantity input args
<?php
/*
* Setting minimum and maximum for quantity input args.
*/
function wc_qty_input_args( $args, $product ) {
$product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
$product_min = wc_get_product_min_limit( $product_id );
$product_max = wc_get_product_max_limit( $product_id );
if ( ! empty( $product_min ) ) {
// min is empty
if ( false !== $product_min ) {
$args['min_value'] = $product_min;
}
}
if ( ! empty( $product_max ) ) {
// max is empty
if ( false !== $product_max ) {
$args['max_value'] = $product_max;
}
}
if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
$stock = $product->get_stock_quantity();
$args['max_value'] = min( $stock, $args['max_value'] );
}
return $args;
}
add_filter( 'woocommerce_quantity_input_args', 'wc_qty_input_args', 10, 2 );
function wc_get_product_max_limit( $product_id ) {
$qty = get_post_meta( $product_id, '_wc_max_qty_product', true );
if ( empty( $qty ) ) {
$limit = false;
} else {
$limit = (int) $qty;
}
return $limit;
}
function wc_get_product_min_limit( $product_id ) {
$qty = get_post_meta( $product_id, '_wc_min_qty_product', true );
if ( empty( $qty ) ) {
$limit = false;
} else {
$limit = (int) $qty;
}
return $limit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment