public function onBeforeActivateCoupon( object $couponarray $args ) : 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

$coupon

(object)  An object containing the details of the coupon code.

$args

(array)  A configuration array, which may contain different attributes according to the section (restaurant or take-away). Here's a list of supported attributes:

  • date - the current date and time for restaurant reservations, the check-in date and time for take-away orders. The date is always formatted in UNIX timestamp;
  • total - the total number of guests for restaurant reservations, the total cost of the items in cart for take-away orders;
  • group - "0" when we are booking restaurant reservations, "1" when we are completing take-away orders.

Return Value

Boolean. Use false to deny the coupon usage.


Example

The example below accepts the activation of a specific coupon code only in case the check-in is 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   object  $coupon  The restaurant reservation to check.
 * @param   array   $args    A configuration array.
 *
 * @return  bool    Use false to deny the coupon code activation.
 */
public function onBeforeActivateCoupon($coupon, $args)
{   
    // make sure we are checking the targeted coupon code
    if ($coupon->code == 'ABCD1234') {
        // get day of the week
        $w = (int) date('w', $args['date']);

        // 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.8 Introduced.
Last Update: 2023-12-29 14:15
Helpful?