Created
December 2, 2024 10:16
-
-
Save tdmrhn/fb46cae0088a9d235b0ae884989af626 to your computer and use it in GitHub Desktop.
Blocksy 2 add discounted price to sale badge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: -{price}