Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Tax Display by Country - Keep shipping prices fixed, no matter the applicable taxes.
- *
- * HOW TO USE THIS CODE
- * Ad the code to the bottom of your theme's functions.php file, and it should run automatically.
- * If you're not familiar with the implementation of custom code on a WordPress site, please refer
- * to the following article: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
- *
- * DISCLAIMER
- * Aelia and any member of its staff are not responsible for any data loss or damage incurred
- * when using the code, which you can use at your own risk.
- *
- * GPL DISCLAIMER
- * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
- * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
- * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
- * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
- * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
- *
- * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
- */
- /**
- * Intercepts a shipping rate added by WooCommerce, to perform the following operations:
- * - Recalculates the shipping cost before tax.
- * - Recalculate the tax.
- * The targe is to ensure that the final cost, inclusive of tax, remains the same no matter
- * what VAT rate applies to it.
- *
- * @param array $rate
- * @param array $args
- * @param WC_Shipping_Method $shipping_method
- * @return array
- */
- add_filter('woocommerce_shipping_method_add_rate', function($rate, $args, $shipping_method) {
- $original_taxes = array_sum($rate->get_taxes());
- // No need to perform a recalculation if either taxes or costs are zero (or negative, in
- // which case they are not valid)
- if(($original_taxes <= 0) || ($rate->get_cost() <= 0)) {
- return $rate;
- }
- $recalc_ratio = $rate->get_cost() / ($rate->get_cost() + $original_taxes);
- $rate->set_cost(round($rate->get_cost() * $recalc_ratio, 2));
- $taxes = ($args['calc_tax'] === 'per_item') ? $shipping_method->get_taxes_per_item($rate->get_cost()) : WC_Tax::calc_shipping_tax($rate->get_cost(), WC_Tax::get_shipping_tax_rates());
- $rate->set_taxes($taxes);
- return $rate;
- }, 999, 3);
Add Comment
Please, Sign In to add comment