Change Sale to % off woocommerce?
Many times we do not want to display the word SALE when that product has a discount program.
It will instead show the percentage (%) off the price of that product.
You can also change the Sale Flash/Sale Badge display for products with sale prices, with the WooCommerce Smart Sale Badge plugin .
The plugin will add the price saved on in-store purchases and add the default string “Sale!” But in this post, I will introduce about adding code

Here is the code to help you do this:
<code>
add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_badge', 20, 3 );
function add_percentage_to_sale_badge( $html, $post, $product ) {
if( $product->is_type('variable')){
$percentages = array();
// Get all variation prices
$prices = $product->get_variation_prices();
// Loop through variation prices
foreach( $prices['price'] as $key => $price ){
// Only on sale variations
if( $prices['regular_price'][$key] !== $price ){
// Calculate and set in the array the percentage for each variation on sale
$percentages[] = round(100 - ($prices['sale_price'][$key] / $prices['regular_price'][$key] * 100));
}
}
// We keep the highest value
$percentage = max($percentages) . '%';
} else {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
$percentage = round(100 - ($sale_price / $regular_price * 100)) . '%';
}
return '<span class="onsale_custom">' . esc_html__( '', 'woocommerce' ) . ' ' . "-". $percentage . '</span>';
}
</code><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span>
</div>
Explain:
add_filter function: hook the add_percentage_to_sale_badge function to an available function in woocommerce as
The code in add_percentage_to_sale_badge is set when the product has a difference between the actual price (
Epilogue
So I have shared how to change Sale to % discount woocommerce.
If you find it interesting, you can follow the wordpress tips section to know more new knowledge.
Follow fanpage to receive the latest posts: Group
Wish you have interesting and interesting knowledge about wordpress!

