Skip to content

Instantly share code, notes, and snippets.

@thetwopct
Last active January 8, 2023 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetwopct/dc4774609104ed9f97212ae5769824b8 to your computer and use it in GitHub Desktop.
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
<?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