Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Taxes  >  Before Use

apply_filters( 'vikrestaurants_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 tax 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. takeaway.item 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.

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('vikrestaurants_before_use_tax', function($use, $id_tax, $options) {
    // init return value if not provided
    $use = !is_null($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 = VikRestaurants::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.3 Introduced.
Ultimo aggiornamento: 2023-12-19
Utile?
Potrebbe interessarti: