I’d like to display the sale price of a product before the regular price. I know this has something to do with get_price_html.
By default, Before Changing ordering of the sale price and original price this outputs something like this.I want to change the output so it looks like this (basically, the two prices are shown in a different order):
Method-1 : Here is the code , Paste this code in your function.php file of your active child theme (or theme).
function PREFIX_woocommerce_price_html( $price, $product ){ return preg_replace('@(.*?).*?(.*?)@misx', '$2 $1', $price); } add_filter( 'woocommerce_get_price_html', 'PREFIX_woocommerce_price_html', 100, 2 );
Method-2 : Here is the code , Paste this code in your function.php file of your active child theme (or theme).
you have to alter get_price_html method which can be done by using following hook’s:
- For Simple Product: woocommerce_get_price_html hook.
- For Variable Product: woocommerce_variation_sale_price_html & woocommerce_variation_price_html
- For Variable Product Min Max Price: woocommerce_variable_sale_price_html & woocommerce_variable_price_html
if (!function_exists('my_commonPriceHtml')) { function my_commonPriceHtml($price_amt, $regular_price, $sale_price) { $html_price = ''; //if product is in sale if (($price_amt == $sale_price) && ($sale_price != 0)) { $html_price .= '' . wc_price($sale_price) . ''; $html_price .= '
'; return $html_price; } } add_filter('woocommerce_get_price_html', 'my_simple_product_price_html', 100, 2); function my_simple_product_price_html($price, $product) { if ($product->is_type('simple')) { $regular_price = $product->get_regular_price(); $sale_price = $product->get_sale_price(); $price_amt = $product->get_price(); return my_commonPriceHtml($price_amt, $regular_price, $sale_price); } else { return $price; } } add_filter('woocommerce_variation_sale_price_html', 'my_variable_product_price_html', 10, 2); add_filter('woocommerce_variation_price_html', 'my_variable_product_price_html', 10, 2); function my_variable_product_price_html($price, $variation) { $variation_id = $variation->variation_id; //creating the product object $variable_product = new WC_Product($variation_id); $regular_price = $variable_product->get_regular_price(); $sale_price = $variable_product->get_sale_price(); $price_amt = $variable_product->get_price(); return my_commonPriceHtml($price_amt, $regular_price, $sale_price); } add_filter('woocommerce_variable_sale_price_html', 'my_variable_product_minmax_price_html', 10, 2); add_filter('woocommerce_variable_price_html', 'my_variable_product_minmax_price_html', 10, 2); function my_variable_product_minmax_price_html($price, $product) { $variation_min_price = $product->get_variation_price('min', true); $variation_max_price = $product->get_variation_price('max', true); $variation_min_regular_price = $product->get_variation_regular_price('min', true); $variation_max_regular_price = $product->get_variation_regular_price('max', true); if (($variation_min_price == $variation_min_regular_price) && ($variation_max_price == $variation_max_regular_price)) { $html_min_max_price = $price; } else { $html_price = '' . wc_price($regular_price) . ''; } //in sale but free else if (($price_amt == $sale_price) && ($sale_price == 0)) { $html_price .= 'Free!'; $html_price .= '' . wc_price($regular_price) . ''; } //not is sale else if (($price_amt == $regular_price) && ($regular_price != 0)) { $html_price .= '' . wc_price($regular_price) . ''; } //for free product else if (($price_amt == $regular_price) && ($regular_price == 0)) { $html_price .= 'Free!'; } $html_price .= ''; $html_price .= '' . wc_price($variation_min_price) . '-' . wc_price($variation_max_price) . ''; $html_price .= '
' . wc_price($variation_min_regular_price) . '-' . wc_price($variation_max_regular_price) . ''; $html_min_max_price = $html_price; } return $html_min_max_price; }