Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

You can develop custom rule conditions. Developing these requires adept programming skills.

How to develop custom rule conditions

You develop a rule condition type and set up a list of conditions of that type. It is similar to the example conditions you see in the rule editor. These serve no actual purpose, but illustrate how it is done.

You add a PHP class in the folder for custom rule conditions. The folder is in the customization folder in

/rule_condition_types/

You pick a name for your condition type (like ‘Example’). The PHP class needs to be called CustomCondition followed by your type name. So for type ‘Example’ you got CustomConditionExample. The file name is called the same (CustomConditionExample.php). The class extends ConfigboxCondition. So you got class CustomConditionExample extends ConfigboxCondition.

There are a few methods that need to be implemented.

  • CustomConditionExample::getConditionsPanelHtml()

  • CustomConditionExample::getConditionHtml($conditionData, $forEditing = true)

  • CustomConditionExample::getEvaluationResult($conditionData, $selections)

getConditionsPanelHtml()

Each type gets a panel in the rule editor. This method returns the HTML to use for it. It essentially contains a list of conditions (the HTML of each condition has a certain structure, you will see more about it in the method getConditionHtml().

getConditionHtml($conditionData, $forEditing = true)

This method takes the condition’s data as parameter and returns the HTML for the condition. You see an example of the HTML in Example’s PHP class, method CustomConditionExample::readMe(). The gist of it is that each condition is wrapped in a <span> Element with HTML 5 data attributes containing the condition’s data. In the <span> is a human-readable name of the condition, the operator to choose from and a text field for the set point, the value the thing you test for should have. Those text fields have the CSS class ‚input’ and a data attribute called ‚data-condition-key’ that holds the key of the entry in the condition data. During saving, those text fields are stored like normal condition data, no need to have them as data in the wrapping <span> element.

Whatever you add as condition data will stored when the rule is stored and will be available to you in the following method.

See CustomConditionExample::readMe() for a line-by-line explanation of the HTML structure.

getEvaluationResult($conditionData, $selections)

This method is called when the condition is evaluated as part of the whole rule. $conditionData contains everything that was set as $conditionData in the previous method. $selections holds an array with the current configuration that is tested.

The method has to return true or false. The method should be as fast as possible. Cache where you can since this method may be called multiple times each time a customer makes a selection in the configurator.

See CustomConditionExample::readMe() for the concrete structure of both parameters.

What is Condition Data

Condition data is an array that has all the data to evaluate the condition during the configuration (via getEvaluationResult()). In the rule editor each value is written as HTML 5 data attribute in the conditions HTML (in getConditionHtml()).

Condition data in the Example type:

Data Key

Data Value

Description

type (required)

Example

The name of your condition’s type

name

Condition 1, Condition 2, Condition 3

Only used for display

fieldname

field_1, field_2, field_3

Arbitrary values to use in evaluating the condition

operator

==

The default operator for evaluating the condition

shouldValue

(empty) – will be populated by the user in the editor

The value the thing to test for should have for the condition to be met

You can have as much condition data in your conditions as you need, the data will be available to you in the method getEvaluationResult().


  • No labels