Created
January 28, 2022 18:00
-
-
Save TakesTheBiscuit/2b0f339883fcd867c3e786427b4b1b45 to your computer and use it in GitHub Desktop.
Woocommerce remove VAT for non GB / UK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Abikething modify pricing display | |
Plugin URI: http://pauldrage.co.uk | |
Description: This plugin over rides the pricing display to handle GB versus ROW for tax. Requires plugin woocommerce_set_country to set $_SESSION wc_country_iso | |
Version: 1.0.0 | |
Author: Paul Drage | |
Author URI: http://pauldrage.co.uk | |
License: GPL2 etc | |
License URI: https:// | |
*/ | |
// Simple, grouped and external products | |
add_filter( 'woocommerce_product_get_price', 'single_price_without_tax', 10, 2 ); | |
add_filter( 'woocommerce_product_get_regular_price', 'single_price_without_tax', 10, 2 ); | |
// Variations | |
add_filter( 'woocommerce_product_variation_get_regular_price', 'var_price_without_tax' , 99, 2 ); | |
add_filter( 'woocommerce_product_variation_get_price', 'var_price_without_tax', 99, 2 ); | |
add_filter( 'woocommerce_show_variation_price', 'var_price_without_tax', 10, 3 ); | |
// Variable (price range) | |
add_filter('woocommerce_variation_prices_price', 'var_price_without_tax', 99, 3 ); | |
add_filter('woocommerce_variation_prices_regular_price', 'var_price_without_tax', 99, 3 ); | |
// Handling price caching (see explanations at the end) | |
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 1 ); | |
function var_price_without_tax( $price, $variation ) { | |
if (!session_id()) { | |
session_start(); | |
} | |
if (isset($_SESSION['wc_country_iso'])) { | |
if (strlen($_SESSION['wc_country_iso']) == 2) { | |
if ($_SESSION['wc_country_iso'] == 'GB') { | |
return round($price,2); | |
} | |
} | |
} | |
$price_excluding_tax = round(($price / 120) * 100,2); | |
return round($price_excluding_tax,2); | |
} | |
function single_price_without_tax( $price, $product=false ){ | |
if (!session_id()) { | |
session_start(); | |
} | |
if ($price) { | |
if (isset($_SESSION['wc_country_iso'])) { | |
if (strlen($_SESSION['wc_country_iso']) == 2) { | |
if ($_SESSION['wc_country_iso'] != 'GB') { | |
return round(($price / 120) * 100,2); | |
} | |
} | |
} | |
} | |
return $price; | |
} | |
function add_price_multiplier_to_variation_prices_hash($hash) { | |
// woocommerce cache for variation prices: | |
// https://developer.woocommerce.com/2015/09/14/caching-and-dynamic-pricing-upcoming-changes-to-the-get_variation_prices-method/ | |
// whilst developing: | |
// WC_Cache_Helper::get_transient_version( 'product', true ); | |
if (!session_id()) | |
{ | |
session_start(); | |
if (isset($_SESSION['wc_country_iso'])) { | |
if (strlen($_SESSION['wc_country_iso']) == 2) { | |
$hash[] = $_SESSION['wc_country_iso'].'_'.get_current_user_id(); | |
// early return | |
return $hash; | |
} | |
} | |
} | |
$hash[] = get_current_user_id(); | |
return $hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment