How to use each option type in the price formula
WooCommerce Product Options supports every type of option inside formulas. We can divide these into 6 categories:
- Numeric options - The Number, Customer Defined Price option types allow customers to enter numbers directly into a field. The numbers that the customer enters can be used directly in the price formula.
- Multiple choice options - The Checkboxes, Image Buttons, Text Labels, and Products (when the display style is set to either Checkboxes or Image Buttons) option types allow customers to select more than one choice at a time. If you wish to use the customer's selections as numeric values in the formula then you must enter this manually for each choice.
- Single choice options - The Radio Buttons, Dropdown Select, Color Swatches, and Products (when the display style is set to either Radio Buttons or Dropdown Select) option types list more than one choice but allow your customers to select only one choice at a time. As with multiple choice options, you must manually enter any Formula Values to be used as numeric values in the formula
- Text options - Text and Paragraph options let you charge by character count, word count or line count in the price formula.
- File upload - Allows customers to upload their own files.
- Date - Allows customers to select a date/time.
Depending on the above category, each option type has a different set of properties that can be used inside a formula.
Option types
Numeric options
Numeric options are the easiest types of option to use in price formulas because the customer directly enters the exact number which will be used in the formula.
Multiple choice options
An option where customers can select more than one choice has the following properties available in the price formula:
- If none selected (
none
) returns1
if none of the choices is selected,0
otherwise. - If any selected (
any
)Â returns1
if any of the choices are selected,0
otherwise. - If all selected (
all
) returns1
if all the choices are selected,0
otherwise. - Count selected (
count
) returns the number of choices being selected. - Min selected value (
min
): when formula values are associated with one or more choices, it returns the minimum value among the choices being selected. - Max selected value (
max
): when formula values are associated with one or more choices, it returns the maximum value among the choices being selected. - Sum of selected value (
sum
): when formula values are associated with one or more choices, it returns the sum of the values across all the choices being selected. - Is choice selected (
choices.<choice_name>.selected
) returns1
when the choice called<choice_name>
is selected,0
otherwise. - Value of selected choice (
choices.<choice_name>.value
) returns the value associated with a specific choice, if it is selected.
Example 1
A store sells mobile devices. Its smartphone single product pages list several accessories as options.
If customers purchase all the accessories listed on the page, then they will receive a 10% discount on the cost of the main device. The 'Accessories' option is a Products option, displayed as image buttons. Let's see what this formula looks like:
if( [Accessories.all], 0.9, 1 ) * [product_price]
If all the choices for the option 'Accessories'Â are selected, then the property [Accessories.all]
returns 1
and the if()
function returns 0.9
, effectively reducing [product_price]
by 10%.
To learn more about the if()
function, you can read the article about logical functions.
Example 2
Like the previous example, the store offers the cheapest accessory for free if customers purchase all the accessories listed on the page. In this case, the formula looks as follows:
[product_price] - if( [Accessories.all], [Accessories.min], 0 )
In this case, if all the choices for the option Accessories are selected, then the [Accessories.all]
property returns 1
and the if()
function returns [Accessories.min]
 – which is the price of the cheapest choice among the accessories – otherwise it returns 0
and the product price remains unaltered.
Single choice options
An option which only allows customers to select one from a list of choices has the following properties:
- If selected (
selected
) returns1
if any choice is selected,0
otherwise. - Selected value (
value
) returns the value associated with the selected choice. - Is choice selected (
choices.<choice_name>.selected
) returns1
when the choice called<choice_name>
is selected,0
otherwise. - Value of selected choice (
choices.<choice_name>.value
) returns the value associated with a specific choice, if it is selected.
Text options
Text and paragraph options have the following properties:
- characters (
characters
) returns the number of characters in the text box, including any non-leading and non-trailing whitespace. - words (
words
) returns the number of words in the text box. - lines (
lines
) returns the number of lines in the textbox (this property only applies to Paragraph options because text inputs are not multiline fields).
File upload options
File upload options have only one property that can be used in price formulas: the count of the files being uploaded by the customers.
This can be inserted in a formula by clicking the button corresponding to the file upload option, or by typing the variable manually. For example, if Logo is the name of the file upload option, then the count property can be written as [Logo.count]
.
Date options
Date options can be used in price formulas, particularly in combination with logical functions. The properties attached to each date option are:
- Number of days from today (
daycount
): the number of days between today and the selected day. Please note thatdaycount
can be zero when selecting today or a negative number when selecting a past date. - Year (
year
): the year of the selected date. - Month (
month
): the month of the selected date, from 1 to 12. - Day (
day
): the day of the selected date, from 1 to 31. - Weekday (
weekday
): the day of the week, where Sundays are1
and Saturdays are7
.
Example
A website sells guided tours of an archaeological site. The price for a ticket during the weekend is 20% higher than on any other day of the week. If "Date" is the name of a Date option, we can write the following formula:
if( or( eq( [Date.weekday], 1 ), eq( [Date.weekday], 7 ) ), 1.25, 1 ) * [product_price]
The product price is multiplied by a factor that depends on the day of the week of the selected date: if the day is a Saturday (7
) or Sunday (1
), then the formula will use 1.25
as a multiplying factor. Otherwise, it will use 1
and the ticket price will stay unaltered.
Adding a numeric value to each choice
If you have created options with pre-defined choices for customers to select from (e.g. checkboxes or radio buttons), then the names of these choices are stored as text rather than as numeric values that can be used in the price formula. As a result, if you want to associate each choice with a numeric value then you need to add this.
To do this, use the blue cog icon in the list of choices to reveal the hidden 'Formula value' column. You can then enter a numeric value which corresponds to each choice.
An example
Let's consider the following example. A shop sells cut-to-size plywood in different thicknesses. Each thickness has a different cost factor and the total cost can be calculated as a product of [Width]
by [Length]
by the thickness factor. The shop owner can list all the different thicknesses as choices of a dropdown select option and associate with each choice a value that will be used in the formula when a given choice is selected.
In that scenario, if Width and Length are the names of the Number options and Thickness is the name of the Dropdown option, then the formula can be written as follows
( [Width] * [Length] / 10000 ) * [Thickness.value]
where the division by 10000 converts square centimeters into square meters.
As you can see, the properties of an option are written using the .
(dot) notation. The formula editor shows a button or a dropdown button depending on the option type to help insert the desired property in the formula. As always, you are also free to type your formula in the text area.