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?

This website uses cookies

This website uses cookies and similar technologies. Some of these technologies are necessary for the proper functioning of the website (Essential). Other technologies are used to evaluate user behavior (Analysis), to integrate external media or for advertising purposes. If you agree, these technologies are activated. For details, please refer to the privacy policy. From this same page you can also update/revoke your consent. If you do not give your consent, only cookies with essential functions will be activated.

We use technologies from the USA on our website. If you give your consent, you therefore consent at the same time to your data being transferred to the USA. We would like to point out that the USA does not have a level of data protection comparable to that of the EU and that any US company can be obliged by state authorities to hand over your data without you having any effective legal protection against this.