English (United Kingdom)

apply_filters_ref_array( 'vikappointments_add_item_cart', bool $accept, VAPCart $cart, VAPCartItem &$item, string &$err )

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

$accept

(bool)  True to add the service into the cart, false to discard it.

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

Example

The following example auto-assigns an unpublished option to the booked service.

/**
 * Trigger hook before adding an item into the cart.
 *
 * @param  boolean  $accept  True to add the item, false otherwise.
 * @param  mixed    $cart    The cart instance.
 * @param  mixed    &$item   The cart item object.
 * @param  string   &$err    String used to raise custom errors.
 */
add_filter('vikappointments_add_item_cart', function($accept, $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 $accept;
}, 10, 4);

Changelog

Version Description
1.0 Introduced.
Last Update: 2021-10-06
Helpful?
See Also: