Untitled
webwizards
plain_text
4 months ago
1.4 kB
19
Indexable
/**
* For vendor coupons, validate minimum spend against only that vendor's
* products in the cart, not the full cart subtotal.
*/
add_filter( 'woocommerce_coupon_validate_minimum_amount', function( $is_below_minimum, $coupon, $subtotal ) {
// Get the vendor who owns this coupon via post_author.
$coupon_vendor_id = (int) get_post_field( 'post_author', $coupon->get_id() );
// If there's no vendor ID (e.g. admin coupon), leave default WC behaviour.
if ( ! $coupon_vendor_id ) {
return $is_below_minimum;
}
$minimum = (float) $coupon->get_minimum_amount();
// No minimum set — nothing to validate.
if ( $minimum <= 0 ) {
return false;
}
// Sum only the cart items that belong to this vendor.
$vendor_subtotal = 0.0;
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$product_vendor_id = (int) marketking()->get_product_vendor( $product_id );
if ( $product_vendor_id === $coupon_vendor_id ) {
$vendor_subtotal += (float) $cart_item['line_subtotal'];
}
}
}
// Return true → minimum not met → WC throws the error.
// Return false → minimum met → validation passes.
return $minimum > $vendor_subtotal;
}, 10, 3 );
Editor is loading...
Leave a Comment