Minimum quantity filter
-
Is there a filter in this plugin that allows changing the minimum quantity in PHP, so that subsequent actions and validations will work?
The page I need help with: [log in to see the link]
-
Yes, you can use our filter hook to change the minimum quantity with PHP.
Example:add_filter( 'wcmmq_single_product_min_max_condition', function( $args, $product ) {
if ( $product->get_id() == 123 ) {
$args['min_value'] = 10;
} return $args;
},10, 2 );This will update the minimum quantity dynamically.
That’s not working for me. I’ve set both
min_valueandmin_qtyto 20, but nothing happens.For reference, here’s my code:
add_filter( 'wcmmq_single_product_min_max_condition', 'ps_vary_minimum_quantity', 10, 2 );
function ps_vary_minimum_quantity( $args, $product ) {
if ( is_null( WC()->cart ) || WC()->cart->is_empty() ) {
return $args;
}
if ( $product->get_slug() != '600ml-bottle-carton-24' ) {
return $args;
}
$categories_to_check = [ 'spring-water-coolers', 'filtered-water-coolers-dispensers' ];
$term_ids = array();
foreach ( $categories_to_check as $term ) {
$term_object = get_term_by( 'slug', $term, 'product_cat' );
if ( $term_object && ! is_wp_error( $term_object ) ) {
$term_ids[] = $term_object->term_id;
}
}
if ( empty( $term_ids ) ) {
return $args;
}
$found = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
if ( $cart_item['variation_id'] > 0 ) {
$product_id = $cart_item['data']->get_parent_id();
}
if ( has_term( $term_ids, 'product_cat', $product_id ) ) {
$found++;
break;
}
}
if ( ! $found ) {
$args['min_value'] = $args['min_qty'] = 20;
}
return $args;
}Any thoughts?
The logic you wrote is correct, but that filter (
wcmmq_single_product_min_max_condition) only affects the product page, not the cart/checkout validations.
So even if you setmin_valueandmin_qty = 20, the cart rules will not change because they use a different validation hook.To make this work, you need to modify both:
✅ Product page filter (you already did)
❗ Cart validation filter – this is the one enforcing the ruleThe plugin uses the following filter for cart checks:
add_filter( 'wcmmq_before_add_to_cart_validation_args', 'your_callback_function', 10, 2 );You need to apply your condition inside this cart-level filter as well. Something like:
add_filter( ‘wcmmq_before_add_to_cart_validation_args’, function( $args, $product ) {
if ( $product->get_slug() !== '600ml-bottle-carton-24' ) {
return $args;
}
$categories_to_check = [
'spring-water-coolers',
'filtered-water-coolers-dispensers'
];
$term_ids = array();
foreach ( $categories_to_check as $term ) {
$term_obj = get_term_by( 'slug', $term, 'product_cat' );
if ( $term_obj && ! is_wp_error( $term_obj ) ) {
$term_ids[] = $term_obj->term_id;
}
}
$found = false;
foreach ( WC()->cart->get_cart() as $item ) {
$pid = $item['variation_id'] > 0 ? $item['data']->get_parent_id() : $item['product_id'];
if ( has_term( $term_ids, 'product_cat', $pid ) ) {
$found = true;
break;
}
}
if ( ! $found ) {
$args['min_value'] = 20;
$args['min_qty'] = 20;
}
return $args;
}, 10, 2 );Thank you for this.
I added the line
add_filter( 'wcmmq_before_add_to_cart_validation_args', 'ps_vary_minimum_quantity', 10, 2 );, but nothing is happening when I add a 600ml bottle or change it quantity when there’s no cooler in the cart.I have working code that uses the
woocommerce_update_cart_validationandwoocommerce_after_calculate_totalsfilters, but it’s not perfect, so I prefer to let your plugin handle the user experience.Can you confirm that your filters work with blocks? If not, is there anything I need to do for this to work?
It looks like this requires a custom solution.
Please reach out to us through our official support channel so we can assist you properly.This forum is limited to support for free features only.
I sent an email to your support team 2 weeks ago and have still not received an answer. Do you know if the latest update enables the filters on blocks?
Please create a ticket at: http://codeastrology.com/my-support/
The topic ‘Minimum quantity filter’ is closed to new replies.