English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Cart  >  Appointments  >  Before Apply Discount

apply_filters( 'vikappointments_before_apply_cart_discount', bool $status, VAPCartDiscount $discount, float $amount, mixed $item )

Fires before calculating the discount to apply.


Description

Trigger hook to let external plugins prevent the application of the discount at runtime. The hook triggers once for each discount to apply and iterates for all the items and options contained within the user cart.

Useful in example to ignore the discount for certain items and options.

NOTE: since the $item instance may vary, it is opportune to check the entity of the object first, such as the following example.

if ($item instanceof VAPCartItem)
{
    $id = $item->getServiceID();
}
else
{
    $id = $item->getID();
}

Parameters

$status

(bool)  True on success, false otherwise.

$discount

(VAPCartDiscount)  The instance holding the discount details.

$amount

(float)  The cost of the item.

$item

(mixed)  The instance holding the details of the item to discount.


Example

The following example avoids to apply a discount for certain services.

/**
 * Trigger hook to let external plugins prevent the application of the
 * discount at runtime.
 *
 * @param  boolean          $status    True on success, false otherwise.
 * @param  VAPCartDiscount  $discount  The discount instance.
 * @param  float            $amount    The amount to discount.
 * @param  mixed            $item      The item instance.
 */
add_filter('vikappointments_before_apply_cart_discount', function($status, $discount, $amount, $item)
{
    // do not apply discount for services with ID 1, 2 and 3
    $list = [1, 2, 3];

    // check whether the service is discountable or not
    if ($item instanceof VAPCartItem && in_array($item->getServiceID(), $list))
    {
        // do not apply discount
        return false;
    }
	
    // apply discount
    return true;
}, 10, 4);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-10-06
Helpful?