public function onBeforeCalculateTotalSaveOrder(VAPCart $cart, object $user) : void

Fires before calculating the order totals.


Description

Trigger hook to manipulate the total cost before it is going to be calculated.

The prices of the cart are strictly related to the taxes and to the discounts. For this reason, starting from 1.7 version, it is no more possible to change the total cost and the user credit at runtime. Any surcharge/discount have now to be applied by using the apposite methods provided by the cart objects.


Parameters

$cart

(VAPCart)  The cart instance.

$user

(object)  An object holding the details of the current user.

Return Value

None.


Example

The following example explains how to apply a custom discount.

/**
 * Trigger hook to manipulate the total cost before it is going to be calculated.
 *
 * @param   VAPCart  $cart  The cart instance.
 * @param   object   $user  The user details.
 *
 * @return  void
 */
public function onBeforeCalculateTotalSaveOrder($cart, $user)
{
    // create new discount rule
    $discount = new VAPCartDiscount(
        $alias = 'custom-discount',
        $amount = 20.00,
        $is_percent = true
    );

    // register discount within the cart
    $cart->addDiscount($discount);
}

Changelog

VersionDescription
1.7 Added $cart argument.
Removed $total and $credit arguments.
1.6 Introduced.
Last Update: 2021-10-08 14:54
Helpful?