Before Apply Discount
apply_filters( 'vikrestaurants_before_apply_cart_discount', bool $apply, Discount $discount, float $amount, Item $item )
Fires before calculating the discount applied by a coupon or a deal to a specific item.
Description
Plugins attached to this hook can prevent the system from applying the discount.
NOTE: calling $discount->apply()
in this hook will result in recursion.
Parameters
- $apply
-
(bool) False to prevent the system from applying the discount to the given item.
- $discount
-
(Discount) The object holding the discount information. This class is part of the
E4J\VikRestaurants\TakeAway\Cart\Deals
namespace. - $amount
-
(float) The initial amount to discount.
- $item
-
(Item) The object holding the details of the item to discount. This class is part of the
E4J\VikRestaurants\TakeAway\Cart
namespace.
Example
The example below prevents the system from discounting all the items under the menu with ID 5.
/**
* Trigger event to let external plugins prevent the application of the
* discount at runtime. Useful in example to ignore the discount for
* certain items and options.
*
* @param bool $apply True to apply the discount, false otherwise.
* @param Discount $discount The discount information.
* @param float $amount The initial amount to discount.
* @param Item $item The item to discount.
*/
add_filter('vikrestaurants_before_apply_cart_discount', function($apply, $discount, $amount, $item) {
// check if the item belongs to "beverages" menu (ID: 5)
return $item->getMenuID() != 5;
}, 10, 4);
Changelog
Version | Description |
---|---|
1.3 | Introduced. |
Last Update: 2023-12-27
Helpful?