A Guide to the Price Functions in WooCommerce

When making modifications to a WooCommerce store it’s common to want to access a product’s price, either to perform a calculation or to display the details on screen.

This may seem like a simple task, but WooCommerce has a lot of price functions and it can be difficult to know which one to use.

We will look at the different price functions in this article and discuss when to use each function.

get_price_html

Briefly – Use this function to return HTML code that shows the product price
How to call the function – The function is called from the product object, so you’ll firstly have to create a product object or have access to a global

$product = wc_get_product(24);
echo $product->get_price_html();

//if you have access to a global
global $product;
echo $product->get_price_html();

Notable things about this function – The function will add tax to the product price if you have specified that prices should display tax in your store. It will also display a different price if the product is on sale.

wc_get_price_to_display

Briefly – Use this function to get a product’s price, including any relevant tax or discounts, the function returns a double so you can use the return value in calculations.

How to call the function – The function is a stand alone function, it takes a product as its first argument and an optional array as it’s a second argument, here’s some example code using a product

$product = wc_get_product(24);
echo wc_get_price_to_display($product) . '<br />';

Using the second array argument, you can calculate a different price, for instance, if you wanted to know what it cost to buy three products at a discount, you could do so by using the following code

$product = wc_get_product(24);
echo wc_get_price_to_display($product,array('price'=>$product->get_sale_price(),'qty'=>3)) . '<br />';

You could use the second argument to calculate the price of a product that does not exist in your catalog, but WooCoomerce insists that the first argument is a product object, you can’t pass null. If you don’t want to fetch an existing product using the wc_get_product function then you can create an empty product and use that. As shown in the example below

echo wc_get_price_to_display(new WC_Product_Simple(), array('price'=>10,'qty'=>1)) . '<br />';

The code above would display the price for a product that cost 10 of the currency units that you use in your WooCommerce store.

Notable things about this function – The wc_get_price_to_display function is an ideal choice if you want to access the display price for a product without any HTML formatting.

get_price

Briefly – Use this function to return a product’s price, the function will return the product’s price without any tax or discount calculations added. If the product is currently on sale, the product’s sale price will be returned.

How to call the function – The function needs to be called from the product object, so you must first create the product object or have access to a global product object.

$product = wc_get_product(24);
echo $product->get_price() . '<br />';

Notable things about this function – The get_price function will return the product’s current price, it will return the product’s sale price if it is currently on sale. The function returns the price as a string without any HTML formatting added so you could use the return value to do further calculations, but it would be safe to cast the value to a double first, please see the code sample below

//rely on PHP to convert the value of get_price()
echo $product->get_price() * 5 . ' is the price times 5<br />';
//specifically cast the result of get_price() to a double
echo (double)$product->get_price() * 5 . ' is the price times 5 after the result of get_price() has been cast to a double<br />';

As you can see from the image below, casting the value has no impact on the result of the sum.

It is also worth bearing in mind that the price returned will not include tax.

get_regular_price

Briefly – Use this function to return a product’s regular price, the function will return the product’s price without any tax or discount calculations added. Even if the product is currently on sale, the product’s non-sale price will be returned.
How to call the function – You must first create the product object via the wc_get_product function or have access to a global product object in order to call the function.

$product = wc_get_product(24);
echo $product->get_regular_price() . '<br />';

Notable things about this function – The get_regular_price function will return the product’s regular (i.e. non-sale) price, it will not return the product’s sale price even if it is currently on sale. Since the function returns the price as a string, you can use it to do further calculations, but keep in mind that the price returned does not include tax and PHP will cast the value returned in order to perform the calculation. Please see the code sample in the get_price section for more information.

get_sale_price

Briefly – Use this function to return a product’s sale price, the function returns the product’s sale price without any tax or discount calculations added. Even if the product is not on sale, the product’s sale price will be returned.

How to call the function – You must either create the product object via the wc_get_product function or have access to a global product object first.

$product = wc_get_product(24);
echo $product->get_sale_price() . '<br />';

Notable things about this function – The get_sale_price function will always return the product’s sale price even if it is not currently on sale. Since the function returns the price as a string without any HTML formatting, you can either rely on PHP to cast the value for you or cast the value yourself, but please remember that the price returned will not include tax. The code sample in the get_price section has more information on this.

Some Examples

Just so you can check my workings, here are a couple of code examples that illustrate what I’ve written in this article.

The code below will show the return values of the various price functions for a product that normally costs 15, costs 10 when on sale in a store where tax is shown on product prices and tax is 1.2 times the cost of a product.

$product = wc_get_product(24);
echo $product->get_name() . "<br>";
echo $product->get_price() . ' get_price<br />';
echo $product->get_regular_price() . ' get_regular_price<br />';
echo $product->get_sale_price() . ' get_sale_price<br />';
echo $product->get_price_html() . ' get_price_html()<br />';
echo wc_get_price_to_display($product). ' wc_get_price_to_display<br />';
echo gettype(wc_get_price_to_display($product)). ' wc_get_price_to_display type<br />';
echo wc_get_price_to_display(new WC_Product_Simple(), array('price'=>10,'qty'=>1)). ' wc_get_price_to_display for single product that costs 10 currency units<br />';
echo wc_get_price_to_display($product, array('price'=>$product->get_sale_price(),'qty'=>1)) . ' wc_get_price_to_display (product is on sale)<br />';

Here’s what the code outputs

Finally in this examples section, let’s look at the types returned by the different functions, so we can see what functions return strings and which functions return numerical values. Here’s the code we could use to do that

$product = wc_get_product(24);
echo gettype($product->get_price_html()) . ' is the get_price_html return type<br />';
echo gettype(wc_get_price_to_display($product)) . ' is the wc_get_price_to_display return type<br />';
echo gettype($product->get_price()) . ' is the get_price return type<br />';
echo gettype($product->get_regular_price()) . ' is the get_regular_price return type<br />';
echo gettype($product->get_sale_price()) . ' is the get_sale_price return type<br />';

Here’s what the code outputs

Final Thoughts

In this article we’ve taken a look at some of the different price functions in WooCommerce and discussed when it’s a good idea to use them.

Hopefully, this will help you the next time you need to work with prices.

If you want to try out any of the code in this article I’d recommend creating a test page inside WordPress, you can find more details of how to do this here.

Learn How To Change Your Store Without Hiring a Coder
Click here to enroll in our Free WooCommerce Coding Course

If you have any questions or queries about the article then please don’t hesitate to drop them in the comments.

Leave a Comment