Skip to content

Instantly share code, notes, and snippets.

@webdados
Created August 28, 2024 09:34
Show Gist options
  • Save webdados/2f0f3207518daa58441a5e898d6a5ccb to your computer and use it in GitHub Desktop.
Save webdados/2f0f3207518daa58441a5e898d6a5ccb to your computer and use it in GitHub Desktop.
Show percentage sales badge for products that are on sale but not with a "Taxonomy/Term and Role based Discounts for WooCommerce" rule applied
<?php
/**
* Taxonomy/Term and Role based Discounts for WooCommerce lets you configure discounts/pricing rules for products based on any WooCommerce product taxonomy terms (built-in or custom), in a very simple way.
* Free version: https://wordpress.org/plugins/taxonomy-discounts-woocommerce/
* PRO Add-on: https://ptwooplugins.com/product/taxonomy-term-and-role-based-discounts-for-woocommerce-pro-add-on/
**/
add_filter( 'woocommerce_sale_flash', function( $html, $post, $product ) {
// Taxonomy/Term and Role based Discounts for WooCommerce (Free) is active?
if ( function_exists( 'WC_Taxonomy_Discounts_Webdados' ) ) {
// No taxonomy rule applies for this product?
if ( ! WC_Taxonomy_Discounts_Webdados()->get_product_applied_rule( $product ) ) {
// Do our HTML
$percentage = round( 100 - ( floatval( $product->get_sale_price() ) * 100 / floatval( $product->get_regular_price() ) ) );
return '<span class="onsale">' . sprintf( '-%d&percnt;', intval( $percentage ) ) . '</span>';
}
}
return $html;
}, 20, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment