How do we properly code the below?
I am looking to have auto-applied coupon in cart for mix-match products and minimum quantity should be 5 but can’t figure it out. I already created a coupon based on specific ID but just want to apply another function for minimum quantity in cart. Thanks
// Hook into the "woocommerce_before_calculate_totals" action
add_action('woocommerce_before_calculate_totals', 'woosuite_apply_coupon_based_on_product_count');
function woosuite_apply_coupon_based_on_product_count() {
$coupon_code = 'Mix Match Bundles'; // coupon code id
$minimum_product_count = 4; // Minimum product count to apply the coupon
$product_count = WC()->cart->get_cart_contents_count();
if ($product_count <= $minimum_product_count) {
if (WC()->cart->has_discount($coupon_code)) {
return; // Coupon is already applied
}
// remove coupon
WC()->cart->remove_coupon($coupon_code);
}
}
New contributor
Cel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1