Using the Price calculation field

NB: The calculation field is only available if the Extra-price add-on is also installed and active.

Available functions

The calculation is run using the MEXP library. You can see the list of available functions here: https://github.com/bugwheels94/math-expression-evaluator

There are several other available functions, specific to the configurator:

Comparison functions:

equals(val1, val2) returns 1 if val1 is equal to val2, 0 if it is not.

not_equal(val1, val2) returns 1 if val1 is not equal to val2, 0 if they are equal.

greater_than(val1, val2) returns 1 if val1 is greater than val2, 0 if it is not.

smaller_than(val1, val2) returns 1 if val1 is smaller than val2, 0 if it is not.

greatest(val1, val2) returns the greatest value of the two.

Rounding:

round_up(val) returns the value rounded up

round_down(val) returns the value rounded down

round_to_nearest(val) returns the value rounded to the nearest integer. E.g. round_to_nearest(1.2) will return 1, round_to_nearest(1.5) will return 2

Others:

value_is_in_range(from, to, val1) returns 1 if val1 is >= from and <= to, 0 otherwise (added in v1.1.16)

count_unique_values(val1, val2, ...) returns the number of unique values passed in the function (this does not use a token but a custom JS function).

Adding your own functions:

If you require other special functions, you can add them using the filter mkl_pc/math-expression-evaluator/tokens and following the Mexp token documentation.

E.g.

add_filter( 'mkl_pc/math-expression-evaluator/tokens', function( $tokens ) {
	$tokens .= '
		mexp.addToken([
			{
				type: 8,
				token: "equals",
				show: "equals",
				value: function (a, b) {
					if(a===b) return 1;
					return 0;
				},
			}
		]);	
	';
	return $tokens;
} );

Example 2: Calculating the surface using the 2 highest values out of 4:

add_filter( 'mkl_pc/math-expression-evaluator/tokens', function( $tokens ) {
	$tokens .= '
		mexp.addToken([
			{
				type: 8,
				token: "calcsurface",
				show: "calcsurface",
				numberOfArguments: 4,
				value: function (a, b, c, d) {
					// Add values to an array
					var t = [ a, b, c, d ];
                    
                    // Sort the array and get the last 2 values (the highest)
					var highest = t.sort(function(aa, bb) {
						return aa - bb;
					}).slice(-2);
                    
                    // calculate the surface using those values
					return highest[0]*highest[1];
				},
			}
		]);	
	';
	return $tokens;
} );

Usage:

length_a to length_d are 4 fields in the same form. We are using the 2 highest values to calculate a surface. Then we multiply by the price per unit (20).

Note: this requires the Extra price add-on v1.1.14+ which has an updated version of the Mexp library.

Was this page helpful?
Leave a Reply