English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Cart  >  Appointments  >  Add Item Option

apply_filters_ref_array( 'vikappointments_add_option_cart', bool $accept, VAPCartItem $item, VAPCartOption &$option )

Fires before adding a new option to a specific item (service).


Description

Trigger hook before adding a new option to a service.

It is possible to use this hook to prevent the customers from adding specific options into the cart or to manipulate the details of the options at runtime.

It is possible to use VAPCart::getInstance() to access all the items stored within the cart.


Parameters

$accept

(bool)  True to assign the option to the service, false to discard it.

$item

(VAPCartItem)  The item to which the option should be added.

&$option

(VAPCartOption)  The option to assign.


Example

The following example increments the maximum quantity of the options for registered users.

/**
 * Trigger event before adding an option to the cart item.
 *
 * @param  boolean  $accept   True to assign the option, false otherwise.
 * @param  mixed    $item     The cart item object.
 * @param  mixed    &$option  The item option object.
 */
add_filter('vikappointments_add_option_cart', function($accept, $item, &$option)
{
    if (VikAppointments::isUserLogged())
    {
        // copy the option details into a new instance because VAPCartOption
        // class doesn't provide a setter to change the maximum quantity
        $option = new VAPCartOption(
            $option->getID(),
            $option->getVariationID(),
            $option->getName(),
            $option->getPrice(),
            $option->getMaxQuantity() * 2,
            $option->isRequired(),
            $option->getQuantity()
        );
    }

    return $accept;
}, 10, 3);

Changelog

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