English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Coupon  >  Before Activate

apply_filters( 'vikappointments_before_activate_coupon', bool $activate, string $scope, object $couponmixed $cart )

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

$activate

(bool)  Use false to deny the coupon usage.

$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 always null while validating the coupon from the back-end.

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  boolean  $activate  Use false to deny the coupon code activation.
 * @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.
 */
add_filter('vikappointments_before_activate_coupon', function($activate, $scope, $coupon, $cart)
{
    if ($scope != 'appointment')
    {
        return $activate;
    }

    // make sure we are checking the targeted coupon code
    if ($coupon->code != 'ABCD1234')
    {
    	return $activate;
    }

    // ignore in case we are calling this hook from the back-end
    if (!$cart)
    {
    	return $activate;
    }

    // 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
	        $activate = false;
	    }
	}

    return $activate;
}, 10, 4);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-07-28
Helpful?