Often, you might want to calculate your product price based on user measurements. And, in some cases, you need to round up the user measurements to specific values in order to return your price. In this article, we’ll look at creating a WooCommerce calculation field using a look up table or pricing grid.

WooCommerce calculation look up tables

We can use calculation fields in the Pro version of the WooCommerce Product Add-Ons Ultimate plugin for complex calculations. And there’s a complete guide to WooCommerce measurement price calculators here.

This image has an empty alt attribute; its file name is wcpaou-cta.png

But, in this specific case, we want to use a look up table to return our prices.

Here’s an example.

Selling made to measure blinds and curtains through WooCommerce

In our example, we’re selling made to measure blinds. The customer enters the width and the height (or drop) required. Then our product calculates the price.

WooCommerce window blinds price calculation

However, we don’t want to use the exact measurements provided by the customer to calculate the price. Instead, we want to round each value up to one of our predefined values then get our price from a pricing table.

The screenshot below of a spreadsheet should help to explain. Across the horizontal x axis on the top row you can see values in centimetres, i.e. 80, 100, 120 etc. These are our values for the width of the blind.

And down the first column, the vertical y axis, you can see values in centimetres again. These are our values for the blind’s height.

WooCommerce pricing table

We can use this grid to return a price based on user measurements. So if the user enters 139cm width by 178cm height, we round up both measurements to the nearest value in the table, which will be 140cm for the width and 180cm for the height.

Then we look across to the 140 column and down to the 180 row to find the price. In this case, the price will be $414.

You can take a look at this in action in the demo product.

Creating add-on fields for a pricing grid calculation field

We need to create two number fields for our width and height measurements. You can take a look at the support document or in the main calculation field article for how to do this.

Now we need to create a calculation field that will use the measurements. To add the calculation field:

  • Click ‘Add Field’
  • Choose ‘Calculation’ in the ‘Field Type’
  • In the ‘Formula’ field enter {look_up_table} – make sure you include the curly brackets
  • In the ‘Action’ field, choose ‘Display As Cost’
WooCommerce look up table calculation

Now we’ve got our fields set up, we need to define our pricing table.

Creating the look up table

Defining the pricing grid needs a little bit of coding ability. We’re going to enter our values in a list using a code snippet. I recommend taking a look at this article on adding snippets to your site.

In this example, I’m using the Code Snippets plugin to add snippets. We need to add two snippets – one to create the pricing table and one to tell our calculation field which table to use.

Here’s the snippet for the pricing table:

<?php
/**
* Create a look up table for calculation fields
*/
function my_prefix_calculation_look_up_tables() {
// The key is the x axis value / the value is a second array where the key is the y axis and the value is the value to be returned
$tables = array(
'wooden_blinds'=> array(
'60' => array(
'60' => 118,
'80' => 131,
'100' => 143,
'120' => 156,
'140' => 169,
'160' => 183,
'180' => 196,
'200' => 210,
'220' => 223,
'240' => 235
),
'80' => array(
'60' => 144,
'80' => 161,
'100' => 178,
'120' => 195,
'140' => 213,
'160' => 230,
'180' => 248,
'200' => 265,
'220' => 282,
'240' => 301
),
'100' => array(
'60' => 178,
'80' => 198,
'100' => 218,
'120' => 241,
'140' => 263,
'160' => 285,
'180' => 307,
'200' => 328,
'220' => 351,
'240' => 372
),
'120' => array(
'60' => 208,
'80' => 232,
'100' => 257,
'120' => 284,
'140' => 309,
'160' => 336,
'180' => 363,
'200' => 388,
'220' => 415,
'240' => 441
),
'140' => array(
'60' => 233,
'80' => 262,
'100' => 291,
'120' => 322,
'140' => 353,
'160' => 383,
'180' => 414,
'200' => 445,
'220' => 475,
'240' => 506
),
'160' => array(
'60' => 271,
'80' => 304,
'100' => 337,
'120' => 371,
'140' => 407,
'160' => 442,
'180' => 477,
'200' => 513,
'220' => 548,
'240' => 582
),
'180' => array(
'60' => 296,
'80' => 334,
'100' => 371,
'120' => 411,
'140' => 450,
'160' => 490,
'180' => 530,
'200' => 568,
'220' => 608,
'240' => 647
),
'200' => array(
'60' => 322,
'80' => 364,
'100' => 406,
'120' => 449,
'140' => 493,
'160' => 537,
'180' => 581,
'200' => 625,
'220' => 669,
'240' => 712
),
'220' => array(
'60' => 360,
'80' => 406,
'100' => 450,
'120' => 499,
'140' => 548,
'160' => 596,
'180' => 644,
'200' => 692,
'220' => 740,
'240' => 789
),
'240' => array(
'60' => 386,
'80' => 435,
'100' => 485,
'120' => 538,
'140' => 591,
'160' => 643,
'180' => 696,
'200' => 749,
'220' => 801,
'240' => 853
),
'260' => array(
'60' => 423,
'80' => 477,
'100' => 531,
'120' => 587,
'140' => 645,
'160' => 702,
'180' => 759,
'200' => 816,
'220' => 873,
'240' => 930
),
'280' => array(
'60' => 449,
'80' => 507,
'100' => 565,
'120' => 627,
'140' => 688,
'160' => 750,
'180' => 811,
'200' => 873,
'220' => 934,
'240' => 995
),
'300' => array(
'60' => 475,
'80' => 537,
'100' => 599,
'120' => 666,
'140' => 732,
'160' => 797,
'180' => 863,
'200' => 929,
'220' => 994,
'240' => 1060
),
),
);
return $tables;
}
add_filter( 'pewc_calculation_look_up_tables', 'my_prefix_calculation_look_up_tables' );

This is in the form of an array:

  • The first key, wooden_blinds, is the name of our look up table. We can include multiple tables with different values in this array provided each has a different name
  • This is also an array where the first level are the values for the x axis on our table
  • Then the value for each x axis is yet another array. This array uses a key=>value system where the key is the y axis value and the value is the price. This is what we actually return to the plugin

Now let’s look at the second snippet:

<?php
/**
* Create a look up table for calculation fields
*/
function my_prefix_calculation_look_up_fields() {
$fields = array(
'1481' => array( // Calculation field ID
'wooden_blinds', // Our table
'1479', // X axis
'1480' // Y axis
)
);
return $fields;
}
add_filter( 'pewc_calculation_look_up_fields', 'my_prefix_calculation_look_up_fields' );

This snippet is a bit simpler. It just maps each look up table to the add-on fields.

  • The first element – in this example, ‘1481’ – is the ID of our calculation field
  • Then for each calculation field element, there’s an array listing the look up table to use, the x axis field ID, and the y axis field ID

You can have multiple calculation fields referring to multiple look up tables.

WooCommerce calculation look up tables – recap

This article requires more coding ability than most other articles on this site. But hopefully you should be able to follow the steps to create your own pricing grids for WooCommerce.

All you need is the Pro version of the Product Add-Ons Ultimate plugin.

This image has an empty alt attribute; its file name is wcpaou-cta.png

Popular Posts

Leave a Reply

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