Created
June 12, 2024 10:31
-
-
Save shameemreza/0b6f0b307e8d44b04987ee058015d51e to your computer and use it in GitHub Desktop.
Show Striked Out Price Range for Variable Products in WooCommerce
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
add_filter('woocommerce_get_price_html', 'display_striked_out_price_for_variable', 200, 2); | |
function display_striked_out_price_for_variable($price='', $product) | |
{ | |
if (!$product->is_on_sale()){ | |
return $price; | |
} | |
if ($product->is_type('variable')) { | |
$variations = $product->get_available_variations(); | |
$regular_prices = array(); | |
$sale_prices = array(); | |
foreach ($variations as $variation) { | |
$variation_obj = wc_get_product($variation['variation_id']); | |
$regular_price = $variation_obj->get_regular_price(); | |
$sale_price = $variation_obj->get_sale_price(); | |
if ($regular_price !== '' && $sale_price !== '') { | |
$regular_prices[] = $regular_price; | |
$sale_prices[] = $sale_price; | |
} | |
} | |
sort($regular_prices); | |
sort($sale_prices); | |
$min_regular_price = count($regular_prices) > 0 ? min($regular_prices) : ''; | |
$max_regular_price = count($regular_prices) > 0 ? max($regular_prices) : ''; | |
$min_sale_price = count($sale_prices) > 0 ? min($sale_prices) : ''; | |
$max_sale_price = count($sale_prices) > 0 ? max($sale_prices) : ''; | |
$regular_price_html = ''; | |
$sale_price_html = ''; | |
if ($min_regular_price === $max_regular_price) { | |
$regular_price_html = wc_price($min_regular_price); | |
} else { | |
$regular_price_html = wc_format_price_range($min_regular_price, $max_regular_price); | |
} | |
if ($min_sale_price === $max_sale_price) { | |
$sale_price_html = wc_price($min_sale_price); | |
} else { | |
$sale_price_html = wc_format_price_range($min_sale_price, $max_sale_price); | |
} | |
return wc_format_sale_price($regular_price_html, $sale_price_html); | |
} | |
return $price; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment