Skip to content

Instantly share code, notes, and snippets.

@tdmrhn
Created December 2, 2024 10:16
Show Gist options
  • Save tdmrhn/fb46cae0088a9d235b0ae884989af626 to your computer and use it in GitHub Desktop.
Save tdmrhn/fb46cae0088a9d235b0ae884989af626 to your computer and use it in GitHub Desktop.
Blocksy 2 add discounted price to sale badge
<?php
add_filter('woocommerce_sale_flash',function ($text, $post, $product) {
if (strpos($text, '<span class="onsale">') === false) {
return $text;
}
if (blocksy_get_theme_mod('sale_badge_value', 'default') === 'custom') {
$text = blocksy_get_theme_mod('sale_badge_custom_value', '-{price}');
if ($product->is_type('variable')) {
$amounts = [];
$prices = $product->get_variation_prices();
foreach ($prices['price'] as $key => $price) {
if ($prices['regular_price'][$key] !== $price) {
$amounts[] = $prices['regular_price'][$key] - $prices['sale_price'][$key];
}
}
if (empty($amounts)) {
$amounts[] = 0;
}
$amount = max($amounts);
} else {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
$amount = 0;
if ($regular_price > 0) {
$amount = $regular_price - $sale_price;
}
}
$formatted_price = wc_price($amount);
$text = str_replace('{price}', $formatted_price, $text );
if ($product->is_type('grouped')) {
$text = $default_text;
}
}
return blocksy_html_tag(
'span',
[
'class' => 'onsale',
'data-shape' => blocksy_get_theme_mod('sale_badge_shape', 'type-2')
],
$text
);
},
10,
3
);
@tdmrhn
Copy link
Author

tdmrhn commented Dec 2, 2024

Usage: -{price}

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