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:

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

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.

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

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