Add Item
public function onAddItemCart(VAPCart $cart, VAPCartItem &$item, string &$err) : bool
Fires before adding a new item (service) into the cart.
Description
Trigger hook before adding a new service into the shopping cart.
It is possible to use this hook to prevent the customers from adding specific services into the cart or to manipulate the booked services at runtime.
Even if the system is configured to ignore the shopping cart, the booked services are still handled by the VAPCart
instance.
Parameters
- $cart
-
(VAPCart) The cart instance.
- &$item
-
(VAPCartItem) The item that is going to be added into the cart.
- &$err
-
(string) When the item is going to be discarded, it is possible to fill this argument with an error message, which will be prompted to the customer.
$err = 'You are not allowed to book this service!'; return false;
Return Value
Boolean. True to add the service into the cart, false to discard it.
Example
The following example auto-assigns an unpublished option to the booked service.
/**
* Trigger hook before adding an item into the cart.
*
* @param mixed $cart The cart instance.
* @param mixed &$item The cart item object.
* @param string &$err String used to raise custom errors.
*
* @return boolean True to add the item, false otherwise.
*/
public function onAddItemCart($cart, &$item, &$err)
{
// create a new custom option
$option = new VAPCartOption(
// the option MUST exists, otherwise it won't be displayed
$id_option = 1,
$id_var = 0,
$name = 'Unpublished option',
$price = 10,
// use 1 in case the customer cannot increase the quantity
$max_qty = 1,
// use true to prevent the customer from deleting the option
$required = false,
$qty = 1
);
// auto-add the option to the new item
$item->addOption($option);
return true;
}
Changelog
Version | Description |
---|---|
1.6 | Introduced. |