6

I am a new member and is weak in programer. I want display Sale price before Regular price (as images attach). 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);

images

Can you help me display Sale price before Regular price?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
donald.sys
  • 301
  • 2
  • 14

2 Answers2

7

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I tested and this works in some products but doesn't work in others. I checked and they are the same product type (subscription or simple product). https://drive.google.com/file/d/1r7T6_Q-AKJO2jprmZE6SaB1pvRaqhNeh/view?usp=drivesdk Example link: https://offer.grow.vn/danh-muc/deal/ – JOY Aug 03 '21 at 04:36
  • Actually I found that it doesn't work with WooCommerce Memberships discount. So if the product was discounted by membership plan, it still shows the Sale Price after Regular Price. – JOY Aug 03 '21 at 08:46
1

You can solve this using only jQuery and swap to element that show the regular price and sale price:

$("#element1").before($("#element2"));

or

$("#element1").after($("#element2"));

:)

and one more on js fiddle https://jsfiddle.net/nak73406/v9k7b5c1/5/

Nasser Ali Karimi
  • 4,462
  • 6
  • 34
  • 77