public function onBeforeApplyCartDiscount(VAPCartDiscount $discount, float $amount, mixed $item) : bool

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

$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.

Return Value

Boolean. True on success, false otherwise.


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   VAPCartDiscount  $discount  The discount instance.
 * @param   float            $amount    The amount to discount.
 * @param   mixed            $item      The item instance.
 *
 * @return  boolean          True on success, false otherwise.
 */
public function onBeforeApplyCartDiscount($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;
}

Changelog

VersionDescription
1.7 Introduced.
Last Update: 2021-10-06 16:38
Helpful?