Last active
November 17, 2018 14:36
-
-
Save kartikparmar/73078280f966757dfd5410fa947d0f78 to your computer and use it in GitHub Desktop.
Setting minimum and maximum for quantity input args
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 | |
/* | |
* 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