Last active
January 8, 2023 05:40
-
-
Save thetwopct/dc4774609104ed9f97212ae5769824b8 to your computer and use it in GitHub Desktop.
Tax Toggle for WooCommerce - How to show From X to X price label on variable products
This file contains 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 | |
/** | |
* Customisation of Tax Toggle for WooCommerce to show From X to X price label on variable products. | |
* | |
* Based on Tax Toggle for WooCommerce 1.3.6 | |
* | |
* Plugin: https://1.envato.market/taxtoggle | |
* | |
* @param string $price Price. | |
* @param object $product Product. | |
* @return string $price Price. | |
*/ | |
function ttp_custom_wootax_update_price_html( $price, $product ) { | |
if ( $product->is_type( 'variable' ) ) { | |
$min_price = $product->get_variation_price( 'min', true ); | |
$max_price = $product->get_variation_price( 'max', true ); | |
if ( $min_price === $max_price ) { | |
return $price; | |
} | |
// setup custom text options. | |
$wc_incl = __( 'Incl.', 'wc-tax' ); | |
$wc_excl = __( 'Excl.', 'wc-tax' ); | |
$wc_zero = __( 'No Tax', 'wc-tax' ); | |
$wc_from = __( 'From', 'wc-tax' ); | |
$wootax_text = get_option( 'wc_tax_text', 'VAT' ); | |
// set default tax word if option is not set. | |
if ( '' === $wootax_text ) { | |
$wootax_text = __( 'VAT', 'wc-tax' ); | |
} | |
$product_tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); | |
$min_price_tax = WC_Tax::calc_tax( $min_price, $product_tax_rates, false ); | |
$max_price_tax = WC_Tax::calc_tax( $max_price, $product_tax_rates, false ); | |
return '<span class="amount product-tax-on product-tax" style="display:none;">From ' . wc_price( $min_price + array_sum( $min_price_tax ) ) . ' to ' . wc_price( $max_price + array_sum( $max_price_tax ) ) . ' ' . $wc_incl . ' ' . $wootax_text . '</span><span class="amount product-tax-off product-tax">From ' . wc_price( $min_price ) . ' to ' . wc_price( $max_price ) . ' ' . $wc_excl . ' ' . $wootax_text . '</span>'; | |
} | |
return $price; | |
} | |
add_filter( 'woocommerce_get_price_html', 'ttp_custom_wootax_update_price_html', 101, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment