Search code examples
phpwordpresswoocommerceproduct

Display the Sale price before Regular price in WooCommerce


I want to display Sale price before Regular price (as shown in image attached).

I determined the hook here is woocommerce_before_variations_form.

Here is the code to edit in the hook.

// define the woocommerce_before_variations_form callback
function action_woocommerce_before_variations_form () {
     // make action magic happen here ...
};
         
// add the action
add_action ('woocommerce_before_variations_form', 'action_woocommerce_before_variations_form', 10, 0);

desired screen layout

Can you help me display Sale price before Regular price?


Solution

  • The following hooked function code will display the Sale price before Regular price:

    add_filter( 'woocommerce_format_sale_price', 'invert_formatted_sale_price', 10, 3 );
    function invert_formatted_sale_price( $price, $regular_price, $sale_price ) {
        return '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>';
    }
    

    Code goes in function.php file of the active child theme (or active theme). Tested and works.