Useful

2018.03.21

Get WooCommerce product variation price and sale price for your pricing table

In categories: Tutorials, Website development, Wordpress

So as usual I was peacefully working on a project of my own using WooCommerce as a base of my e-commerce solution. I had previous experience building standard shops consisting of multiple products with variable prices for each variation. But now I was creating a website with a single downloadable product that had multiple variations and each variation a custom price.

Now on the main product page I had to create a pricing table for this variable product to show off different pricing plans. And this is where I came across a problem of how to separately retrieve or get WooCommerce product variation price and sale price. I know that it sounds like an easy task and I assumed that WooCommerce definitely would have prepared such a function but I was mistaken.

In order to display custom product variation price I created a function with 2 attributes:

  1. $product_id – The ID of the variable product
  2. $variation_id – The ID of the variation that you need to get the price

So here is the function. You should add this in your functions.php file:

And after you have added the function you can easily use it to display WooCommerce product variation regular price an variation sale price on your website. Add this code where you want the prices to be displayed. Change 9 to your product ID and 15 to your product variation ID.

If you are wondering where to get ID values of variations then you can take a look in the screenshot below. Product Variation ID values are right next to variation labels.

Now you will be able to create custom pricing tables using Wordpres and WooCommerce and will not have to worry if you update product variation prices – they will be also updated and displayed in the pricing table. Please let me know your thoughts below and if you would like to get some extra information about the product variations.

Nauris Kolāts

Nauris is a freelance designer / developer who loves to dig into the UX as much as in the ground for fishing worms. And fishing is just one amongst the long list of his active lifestyle hobbies.

Other posts

Your thoughts

 

Latest work