I have setup custom quantity inputs for simple products, which works as intended, but it breaks on variable products, so I want to get it to work on each variation of a variable product, but I can’t figure out how
Simple product code:
add_filter( 'woocommerce_quantity_input_args', 'productQuantityInputArgs', 10, 2);
function productQuantityInputArgs( $args, $product ) {
if ( ! is_cart() ) {
$product = wc_get_product();
$customPasirinkimoKiekis = $product->get_meta('_quantity_product_in_package');
$stock = $product->get_meta('_stock_quantity');
$productType = $product->get_type();
if($productType === 'simple') {
$args['max_value'] = $stock - ($stock % $customPasirinkimoKiekis);
$args['input_value'] = $customPasirinkimoKiekis;
$args['min_value'] = $customPasirinkimoKiekis;
$args['step'] = $customPasirinkimoKiekis;
}
}
return $args;
}
Removing the if type === ‘simple’ breaks variable product pages, but each variation of product has the same needed meta data as simple products and I can’t figure out how to do it for variable/variations of products
2
Answers
When modify the quantity input for variable products in WooCommerce, always think that each variation of a variable product can have its own settings.
Please check with following code of filter is working for variable product :
To handle all product types based on a custom field that changes product quantity input arguments, you need something more complete.
Replace all your related code with the following:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.