This snippet blocks one coupon code on delivery orders and leaves it valid on pickup orders. It reads the order type that FoodMaster (formerly WooFood) stores in the WooCommerce session. Add it to your child theme’s functions.php file, or to a code snippets plugin.
Disable one coupon code for delivery orders
Updated for current FoodMaster versions: the code below uses the foodmaster_ names. Older installs that still run WooFood used the woofood_ prefix, so on those change foodmaster_ back to woofood_.
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( 'foodmaster_order_type') == "delivery")
{
return false;
}
}
}
}
return $is_valid;
}How the snippet works
- It hooks into
woocommerce_coupon_is_valid, which decides whether a coupon can be applied. - It compares the coupon with the code you put in
$coupon_code_to_apply. Replace"YOURCOUPONCODEHERE"with your own coupon code. - If the session holds
foodmaster_order_typeasdelivery, it returnsfalseand the coupon is rejected. - On pickup orders, and for every other coupon, it returns the value it was given, so the coupon still works.
To check it worked, add an item to the cart, choose delivery and try the coupon. It should be refused. Switch the order to pickup and the same coupon should apply.
FoodMaster (formerly WooFood)
The snippet relies on the order type stored by FoodMaster (formerly WooFood), the restaurant ordering plugin for WooCommerce, which handles pickup and delivery orders. You can get FoodMaster here.
