WooCommerce: How to Calculate Product Prices with Custom Formulas

WooCommerce is a powerful and feature-rich WordPress plugin. But despite the fact it’s got many features, there is always something missing for custom ecommerce businesses.

The problem with “all-you-can-do plugins” is the same as “all-in-one WordPress themes“: the more custom functionalities a piece of software has, the worse its performance gets.

Therefore, there is no need to pack every imaginable functionality into WooCommerce as it is easily expandable with other plugins.

For example, one particular feature is not available in WooCommerce out of the box: the possibility to use math formulas to calculate product prices.

This functionality is crucial for specific industries such as digital printing, custom-sized furniture, curtains, tiles, rollers and so on.

Curious to know how this can be implemented?

The idea and purpose of flexible price calculation

Quite often, businesses need to use some variables for a proper and accurate price calculation. It is relevant for products with measurements like width or height. Typically these are advertisement banners and other printing products, rollers and shutters etc.

Another use case would be when a product involves customization, e.g. ingredients or services, so a customer is allowed to choose add-ons. A perfect example would be a food ordering form, like pizza or cakes. Usually, the price for such products is based on quantity and type of ingredients added. 

Other examples: personalized medals, gift wrappings, custom cut glasses/mirrors or even online game user rank upgrades!

There is a handy plugin for that

Uni CPO WordPress plugin lets you use math formulas for product prices – any formula! Whatever you put inside the ‘main formula’ setting becomes the product’s price.

Leave it empty and you will get a ‘0.00‘ price; fill it with any valid math expression and you can “build” your product price. Putting only a number also works (e.g. ‘42‘).

Math formula can be as simple as the following one:

{uni_cpo_width}*{uni_cpo_height}*0.12

Or it may be as complex as:

({uni_cpo_width}*{uni_cpo_height}*{uni_nov_cpo_base_price})+{uni_cpo_thickness}*2.12+round(max({uni_cpo_acc1}, {uni_cpo_acc2}, {uni_cpo_acc3}))

Or even more complex! 

Please note: variables inside the curly brackets are product custom fields. Those that begin with ‘uni_cpo_‘ are regular variables from the custom options added to the product. 

Which brings us to the next note: Uni CPO plugin also lets you add custom product options by using a visual form builder.

Each regular variable holds the value of its option. Those that start with ‘uni_nov_cpo_‘ are numeric variables, created without any connection to options. It is possible to create a lot of artificial variables – in the plugin I call them NOVs (abbr for “non-option variables“) – and each can hold its part of the formula.

It is also possible to re-use NOVs in other NOVs and create a cascading effect.

Custom functions and NOV matrixes

You may have noticed that I used known math functions in the previous examples: ‘round‘ and ‘max‘. That’s right, and you can use these are functions in your formulas in the same way as you may do that in spreadsheet software.

There are more! Functions like ‘round‘, ‘floor‘ and ‘ceil‘ help with rounding. Functions such as ‘min‘ and ‘max‘ accept several arguments and make it possible to get the lowest and the highest number from a set of options. There are also ‘IF‘ statements. You can compare the option’s value against some threshold and determine if it is higher or lower and build additional logic on this.

NOV matrixes can be one or two dimensional and are used to derive a new value based on the value(s) from regular option(s) or NOV(s).

The simplest example is table-based pricing. Let’s say you have an option called “Thickness” (a dropdown of available values). Each thickness value (e.g. 4mm, 6mm etc.) has a specific price (e.g. 4mm has price $5.10, 6mm has price $8.79).

In this case, {uni_nov_cpo_thickness} will be holding thickness values (4 and 6 mm):

…and {uni_nov_cpo_thickness_price} will be holding price values:

Each option includes sub-options such as nicename, slug and price/rate settings (in our example these will be used instead of ‘1’ and ‘2’ that really don’t mean anything):

Demo and usage examples

Uni CPO demo site features many case studies – for example “Foamex banner” or “Furniture” products. They all take advantage of math formulas in order to calculate their prices.

The potential of the Uni CPO plugin is quite vast. You can use it even for product add-ons and for product configurations.
Another Uni CPO case study: measurement price calculations

Free vs PRO version

The free version of Uni CPO gives you the option to use math formulas and math functions in your product price calculations. Non-option variables are also available.

Uni CPO PRO introduces matrix functionality; therefore, it gives a possibility to create table-based pricing. Besides, it enables the product’s weight calculation and allows defining its measures; these values could be used during shipping price calculation (if your shipping plugin supports these values to be set dynamically).

The pro version includes updates and support (first response is always within 24 hours) for one year or lifetime, depending on the chosen license.

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

4 thoughts on “WooCommerce: How to Calculate Product Prices with Custom Formulas

  1. I did it with my custom codes with php. I made calculator and user can add extra values to current product price. But I don’t know how can I add product to cart with user’s own price. This plugin does it, but I need with custom codes. Can you share tutorials about it?

    1. Hey Cavid, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. This looks like a really useful Plugin. I will have to try it on a cabinet making site I am working on.

    1. Cool – let me know how it goes!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *