Last active
August 12, 2024 17:25
-
-
Save helpercode0/79a568163af6435206d21b2954d72f94 to your computer and use it in GitHub Desktop.
Include Exclude Tax Toogle For Woocommerce
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 | |
function kl_tax_toggle_shortcode() { | |
$kl_show_tax = isset($_COOKIE['kl_show_tax']) ? $_COOKIE['kl_show_tax'] : 'yes'; | |
ob_start(); | |
?> | |
<div class="kl-tax-toggle-wrapper"> | |
<a href="javascript:void(0)" id="kl-hide-tax-btn" data-show-tax="no" class="<?php echo $kl_show_tax === 'no' ? 'active' : ''; ?>"> | |
Exclude | |
</a> | |
<a href="javascript:void(0)" id="kl-show-tax-btn" data-show-tax="yes" class="<?php echo $kl_show_tax === 'yes' ? 'active' : ''; ?>"> | |
Include | |
</a> | |
</div> | |
<script type="text/javascript"> | |
document.addEventListener('DOMContentLoaded', function() { | |
var showTaxButton = document.getElementById('kl-show-tax-btn'); | |
var hideTaxButton = document.getElementById('kl-hide-tax-btn'); | |
showTaxButton.addEventListener('click', function() { | |
document.cookie = 'kl_show_tax=yes;path=/'; | |
kl_refreshCart(); | |
}); | |
hideTaxButton.addEventListener('click', function() { | |
document.cookie = 'kl_show_tax=no;path=/'; | |
kl_refreshCart(); | |
}); | |
function kl_refreshCart() { | |
jQuery('body').trigger('wc_fragment_refresh'); | |
location.reload(); | |
} | |
}); | |
</script> | |
<?php | |
$output = ob_get_clean(); | |
return $output; | |
} | |
add_shortcode('kl_tax_toggle', 'kl_tax_toggle_shortcode'); | |
function kl_clear_cart_prices_on_toggle() { | |
if ( isset($_COOKIE['kl_show_tax']) ) { | |
if ( isset($_GET['kl_refresh_cart']) && $_GET['kl_refresh_cart'] === '1' ) { | |
WC()->cart->calculate_totals(); | |
WC()->cart->set_session(); | |
wc_add_notice('Cart prices updated based on tax toggle.', 'success'); | |
} | |
} | |
} | |
add_action('init', 'kl_clear_cart_prices_on_toggle'); | |
function kl_add_refresh_param_to_urls( $url ) { | |
if ( isset($_COOKIE['kl_show_tax']) ) { | |
$url = add_query_arg('kl_refresh_cart', '1', $url); | |
} | |
return $url; | |
} | |
add_filter('woocommerce_get_checkout_url', 'kl_add_refresh_param_to_urls'); | |
add_filter('woocommerce_get_cart_url', 'kl_add_refresh_param_to_urls'); | |
function kl_display_prices_including_tax() { | |
$kl_show_tax = isset($_COOKIE['kl_show_tax']) ? $_COOKIE['kl_show_tax'] : 'yes'; | |
return $kl_show_tax === 'yes'; | |
} | |
add_filter('woocommerce_cart_display_prices_including_tax', 'kl_display_prices_including_tax'); | |
function kl_adjust_shop_prices_based_on_tax_toggle( $price, $product ) { | |
$kl_show_tax = isset($_COOKIE['kl_show_tax']) ? $_COOKIE['kl_show_tax'] : 'yes'; | |
if ( $kl_show_tax === 'no' ) { | |
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); | |
$taxes = WC_Tax::calc_tax( $product->get_price(), $tax_rates, true ); | |
$price_excl_tax = wc_price( $product->get_price() - array_sum( $taxes ) ); | |
$price = $price_excl_tax." excl tax"; | |
} | |
return $price; | |
} | |
add_filter( 'woocommerce_get_price_html', 'kl_adjust_shop_prices_based_on_tax_toggle', 10, 2 ); | |
add_filter( 'woocommerce_get_variation_price_html', 'kl_adjust_shop_prices_based_on_tax_toggle', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment