Disable Coupon for Delivery Orders
June 20, 2023
By utilizing this function, the code ensures that the coupon remains valid when the order type is set to “pickup” or if the user is operating within the admin section. This way, the coupon’s default validity status remains unaffected.
Feel free to integrate this code snippet into your child theme’s functions.php
file. However, it’s crucial to undertake thorough testing on a staging environment before applying the code to your live website. This precaution helps prevent unexpected issues from arising.
Make sure to replace "YOURCOUPONCODEHERE"
with the actual coupon code you intend to restrict. This code customization, coupled with careful testing, ensures that your WooCommerce store operates seamlessly according to your desired functionality.
add_filter( 'woocommerce_coupon_is_valid', 'wpslash_hook_disable_coupon_code_for_delivery', 10, 3 );
function wpslash_hook_disable_coupon_code_for_delivery( $is_valid, $coupon, $discount ){
if (!is_admin())
{
$coupon_code_to_apply = "YOURCOUPONCODEHERE";
if($coupon == $coupon_code_to_apply)
{
if(WC()->session)
{
if (WC()->session->get( 'woofood_order_type') == "delivery")
{
return false;
}
}
}
}
return $is_valid;
}