//Apply discount coupon automatically in woocommerce
add_action('woocommerce_before_cart_table', 'discount_when_quantity_greater_than_10');
function discount_when_quantity_greater_than_10()
{
global $woocommerce;
global $total_qty;;
$vip_coupon = 'zt127fbb0383';
//get all applied coupons
$applied_coupons = $woocommerce->cart->get_applied_coupons();
//check if the coupon is already applied or not
$check = in_array($vip_coupon, $applied_coupons);
//if not applied then apply it automatically or return
if (!$check) {
// if( $total_qty > 10 ) {
$coupon_code = $vip_coupon;
if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>The number of Product in your order is greater than 10 so a 10% Discount has been Applied!</strong></div>';
// }
}
}
0 Comments
Post a Comment