I am trying display a custom price range for my variable products.
I managed to insert a price range with regular (min and max) prices and sale (min and max) prices.
Here is my code attempt:
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// Main Price
$regular_priceMin = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
$regular_priceMax = $product->is_type('variable') ? $product->get_variation_regular_price( 'max', true ) : $product->get_regular_price();
$sale_priceMin = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();
$sale_priceMax = $product->is_type('variable') ? $product->get_variation_sale_price( 'max', true ) : $product->get_sale_price();
if ( $regular_priceMin !== $sale_priceMin && $product->is_on_sale()) {
$price = '<p class="teste"><del>' . wc_price($regular_priceMin). 'a' . wc_price($regular_priceMax) . '</del></p> <ins>' . wc_price($sale_priceMin) . '</ins>';
}
return $price;
}
However some sale prices have the same values and the formatting is not correct.
It creates 3 lines:
- One for the minimum price value,
- another for the letter "a"
- and another for the maximum price value.
How can I organize this correctly?
The tag <del>
tag is not in the same line.
How can solved this? What I am doing wrong?
2
Answers
Would do you not use standart
$product->get_price_html()
that is available for all type products?Try the following revisited code that will work everywhere on product loops except on single products for variable products custom price range:
Code goes in function.php file of your active child theme (or active theme). Tested and works.
A variable on sale product price: