.
PRODUCTS WORDPRESS PLUGINS

How to get WooCommerce Regular and Sale prices with or without a post ID

4 minute read, 3 if you're quick

If you simply just want to get to the commands, that's cool here are the links to take you right there. If you prefer to read what the commands do, keep reading...

Display Regular and Sale prices on the Product page

Display Regular and Sale prices within the Functions.php file or plugin

Let's say you need to get Regular and Sale prices from a WooCommerce product from within a product page - therefore you know the product ID - or from somewhere external to a product page, like your functions.php file or maybe a plugin you are working on.
Below I will show you how you can find out if the product has a Sale price and the quickest/easiest way to get the data you need depending on either of the situations above. These commands are written in PHP as per most of WordPress.

I like to explain things using examples, it just helps to understand the code. Let's start off by displaying the product Sale or Regular price somewhere on a Product page.

Display Regular & Sale Prices on the Product page

Now, most WooCommerce themes should have this already coded in so in the real world you probably wouldn't need to add code to display the Sale price, but, I will use it just to explain how to do it just in case you need to display the price somewhere else on the page.

For this example, let's say you would like to display the Sale price next to the stock level on the WooCommerce product page.
Now the nice people at WordPress have created some simple commands to use which makes it easy as peas! No need for any SQL SELECT commands and all that fluff here!

First, you need to make sure your file - I will be using stock.php within WooCommerce - has the Global command to get the product data (it most likely has, but check anyway).

I have added two versions of the same command, one to get the value into a variable and the other just echo it out the screen.

//Add the global command to get the product data
global $product;

//Add the two field strings to a variable
$price_regular = $product->regular_price;
$price_sale = $product->sale_price;

//Display the two field strings to the screen
echo $product->regular_price;
echo $product->sale_price;

Back to top ☝️

Display Regular & Sale Prices from Functions.php or Plugin

If you wish to use a function/class from within the Functions.php file or maybe you are creating a plugin and need to get this data, this is how you do it.

Once again WordPress have made it nice and easy to get this information. Below are the commands you need.
First off you need to get the whole post data using the wc_get_product command, if you display the output of this command you will see all the elements to it (I have included the command to display them below).

Once you have this data all you need to do is choose which element you wish to display, in this example get_regular_price and get_sale_price.

//Get the whole post/product data
$product = wc_get_product( $av->post_id );

//Display the elements from this command
echo "<pre>", print_r( $product ), "</pre>";

//Add the two field strings to a variable
$reg_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();

//Display the two field strings to the screen
echo $product->get_regular_price();
echo $product->get_sale_price();

Back to top ☝️



And that is it, very simple to do.
Happy coding!

^