Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the Sale price before Regular price in WooCommerce

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?

like image 465
donald.sys Avatar asked Feb 17 '19 03:02

donald.sys


People also ask

How do you display sale price?

For example, a $20 item discounted to $15 should display “25% off” (and not “$5 off”), whereas items priced above $100 should generally display the discount in absolute numbers; for instance, a $1,000 item discounted to $900 should display “$100 off” (and not “10% off”).

How do I change the price position in WooCommerce?

This is what we need to change the price position. Create a new JS by clicking on "Custom CSS & JS > Add Custom JS". Copy and paste the JS code below and save the JS code. You're done, now you can see the price position has moved from top to page bottom.

How do I change the dynamic price display in WooCommerce?

Go to: Product > Product X (the product you wish to set a rule for). Next, go to: Product Data > Dynamic pricing. Select Add Pricing Group. There, configure the conditions for the rule.

How do you find the sale price in WooCommerce?

Get the price of a simple procut php #the product must be instantiated above like $product = new WC_Product(); echo $product->regular_price; echo $product->sale_price; ?> If the product has variations you will get nothing if you will use the code above. You need to get the variation product prices.


2 Answers

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.

like image 57
LoicTheAztec Avatar answered Sep 28 '22 05:09

LoicTheAztec


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/

like image 22
Nasser Ali Karimi Avatar answered Sep 28 '22 04:09

Nasser Ali Karimi