Before Activate
public function onBeforeActivateCoupon(string $scope, object $coupon, mixed $cart) : bool
Fires while checking whether the specified coupon code can be redeemed or not.
Description
This filter can be used to apply additional conditions to the coupon validation.
When this hook is triggered, the system already validated the standard conditions and the coupon has been approved for the usage.
In case of positive result, the customer will be allowed to redeem the specified coupon code.
Parameters
- $scope
-
(string) For which entity we are redeeming the coupon (appointment, package or subscription).
- $coupon
-
(object) An object containing the details of the coupon code.
- $cart
-
(mixed) An instance holding the cart details, which varies according to the current group.
NOTE: this argument is alwaysnull
while validating the coupon from the back-end.
Return Value
(bool) Use false to deny the coupon usage.
Example
The example below accepts the activation of a specific coupon code only in case all the booked services have a check-in between Monday and Thursday.
/**
* This filter can be used to apply additional conditions to the
* coupon validation. When this hook is triggered, the
* system already validated the standard conditions and the
* coupon has been approved for the usage.
*
* @param string $scope For which entity we are redeeming the coupon.
* @param object $coupon The restaurant reservation to check.
* @param array $args A configuration array.
*
* @return boolean Use false to deny the coupon code activation.
*/
public function onBeforeActivateCoupon($scope, $coupon, $cart)
{
if ($scope != 'appointment')
{
return true;
}
// make sure we are checking the targeted coupon code
if ($coupon->code != 'ABCD1234')
{
return true;
}
// ignore in case we are calling this hook from the back-end
if (!$cart)
{
return true;
}
// iterate all the booked services
foreach ($cart->getItemsList() as $items)
{
// get day of the week (true adjusts to the local timezone)
$w = (int) $item->getCheckinDate('w', $local = true);
// check if the day of the week is Fri (5), Sat (6) or Sun (0)
if (in_array($w, [0, 5, 6]))
{
// prevent coupon activation
return false;
}
}
return true;
}
Changelog
Version | Description |
---|---|
1.7 | Introduced. |