English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Taxes  >  Before Use

apply_filters( 'vikappointments_before_use_tax', mixed $use, int $id_tax, array $options )

Fires before assigning the taxes to apply.


Description

Trigger hook to allow external plugins to switch tax ID at runtime, which may vary according to the specified options.


Parameters

$use

(bool|int)  The new ID of the tax to apply or false to ignore the taxes calculation.

$id_tax

(int)  The default ID assigned to the specified object.

$options

(array)  An associative array of options.

  • subject - the entity to which the TAX refers. When specified, the $id_tax argument is equals to the ID of the given subject (e.g. service with ID 7). In that case, the system will load the tax ID assigned to the specified record.
  • lang - the language tag specified by the user.
  • id_user - the ID of the customer that is going to purchase something (missing when id_employee is specified).
  • id_employee - the ID of the employee that is going to purchase a subscription (missing when id_user is specified).

Example

The example below switches at runtime the taxes to use according to the type of customers. Tax #1 will apply to those customers that don't own a VAT number, otherwise tax #2 will be applied.

/** 
 * Trigger hook to allow external plugins to switch tax ID at
 * runtime, which may vary according to the specified options.
 *
 * @param  mixed    $use      The ID of the tax to apply or false
 *                            to ignore the taxes calculation.
 * @param  integer  $id_tax   The current tax ID.
 * @param  array    $options  An array of options.
 */
add_filter('vikappointments_before_use_tax', function($use, $id_tax, $options)
{
    // init return value if empty
    $use = $use ? $use : $id_tax;

    if (!isset($options['id_user'])
    {
        // use default taxes if we are not dealing with a customer
        return $use;
    }

    // load customer details
    $customer = VikAppointments::getCustomer($options['id_user']);

    if (empty($customer->vatnum))
    {
        // customers without a VAT number, use tax #1
        $use = 1;
    }
    else
    {
        // otherwise use tax #2
        $use = 2;
    }
    
    return $use;
}, 10, 3);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-08-18
Helpful?