Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyskoczilova/2ff56afcfd4c75338fb7f0be3a5615c2 to your computer and use it in GitHub Desktop.
Save vyskoczilova/2ff56afcfd4c75338fb7f0be3a5615c2 to your computer and use it in GitHub Desktop.
Tax placeholders for Price Display Suffix
<?php
/**
* @snippet Tax placeholders for Price Display Suffix | WooCommerce
* @comment Use "{tax_rate}" and "{tax_rate_label}" placeholder for displaying the product tax rate and its label
* @source https://gist.github.com/vyskoczilova/2ff56afcfd4c75338fb7f0be3a5615c2
* @author Karolína Vyskočilová (https://kybernaut.cz)
* @testedwith WooCommerce 4.2
*/
// -------------------
add_filter( 'woocommerce_get_price_suffix', 'replacement_for_tax_class_name', 99, 4 );
function replacement_for_tax_class_name( $html, $product, $price, $qty ){
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
$tax_rate_value = '';
$tax_rate_label = '';
if(!empty($tax_rates)){
foreach($tax_rates as $tax ){
$tax_rate_value = rtrim($tax['rate'], "0");
$tax_rate_value = rtrim($tax_rate_value, ".");
$tax_rate_label = $tax['label'];
}
}
$replacements = array(
'{tax_rate}' => $tax_rate_value,
'{tax_rate_label}' => $tax_rate_label,
);
$html = str_replace( array_keys( $replacements ), array_values( $replacements ), $html );
return $html;
}
@Ansolon
Copy link

Ansolon commented Jun 13, 2020

very thanks i exactly looking for it days

@vyskoczilova
Copy link
Author

@Ansolon glad it helped :) (I assume that you have the newest WooCommerce so I bumped the version)

@Ansolon
Copy link

Ansolon commented Jun 19, 2020

Yes its working on wordpress 5.4.2 and woocommerce 4.2.0
Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment