Call Us: +1-888-227-1645

How to Change the Display of a Variable Product Price in a Custom Woocommerce Theme

When building a custom WooCommerce theme, you may come across some things that are tricky to implement. To further complicate matters, some of the available documentation may be lacking. Woocommerce is built to be plug and play, and for the most part that’s true. However, occasionally you’ll find yourself trying something a bit out of the box that goes against the default WooCommerce way of doing things. On a recent project, I came across one of these situations.

The Situation

I wanted to get the numerical value of the lowest variation price, in order to be able to manipulate it when certain variation options are selected. Specifically, I wanted to have the price read “Priced from $XX” until all of the product attributes are selected, then display the final price. The standard default WooCommerce function to get a product’s price is the following function:

$product->get_price_html()

However, the issues with this are twofold:

Theoretically, you can try extracting the first price, rip out the formatting, etc. But if this structure ever changes, your application would break in the future. This structure actually did change in a recent WooCommerce update. I knew there must be a better way to get the price data, minus the extra formatting.

The Solution

After doing some digging, I found two very useful functions:

$product->get_variation_price( 'min' )
$product->get_variation_price( 'max' )

These two functions return the minimum and maximum values respectively for a variation product price in a numerical format. Here, the

$product->get_variation_price( 'min' ) 
was just what I needed, allowing me to easily get this value without having to jump through a bunch of hoops. Once I had this, I could manipulate it as needed, and then display the new price (adding formatting back in as needed). In my case, this function was perfect for my goal of wanting to display, “Priced from $XXX” before a variation product’s price when first viewing the product.

I hope this simple tip was helpful if you ever come across this requirement in one of your projects. And if you're ever in need of WooCommerce development solutions, don't hesitate to reach out to Curious Minds. Our experienced team can provide a wide range of services, from headless CMS development and professional SEO optimization to enterprise WordPress development solutions. Contact us today to learn more and take your web presence to the next level.

Happy Coding!

Let's build something amazing together

Give us a ring and let us know how we can help you reach your goals. Or if you'd like, start a chat. We're usually available 9-5 EST. We try to respond to every inquiry within one business day.

Phone number
+1-888-227-1645

Technologies and services we work with:

Laravel Laravel
WordPress WordPress
React ReactJS
EmberJS EmberJS
woocommerce WooCommerce
next.js NextJS
gatsby Gatsby
Shopify Shopify
VueJs VueJS
contentful Contentful
next.js JAMStack
gatsby Laravel Jigsaw
WPEngine WP Engine
Laravel Livewire Laravel Livewire
Netlify Netlify