Add operators

We can add our own operators. The format for creating an operator is simple and is as follows:

$arrays_operators = array(
       'KEY1'     =>'VALUE SHOW1',
       'KEY2'     =>'VALUE SHOW2',
   );

Example 1:

$arrays_operators        = array(
           'IN'     => __( 'IN', 'wad' ),
           'NOT IN' => __( 'NOT IN', 'wad' ),
       );

Example 2:

$number_operators        = array(
           '<'  => __( 'is less than', 'wad' ),
           '<=' => __( 'is less or equal to', 'wad' ),
           '==' => __( 'equals', 'wad' ),
           '>'  => __( 'is more than', 'wad' ),
           '> =' => __( 'is more or equal to', 'wad' ),
           '%'  => __( 'is a multiple of', 'wad' ),
       );

So we can create our own operators by following the same rule thanks to the “wad_operators_fields_match” filter. In the application example (the second part of this document) a complete use of the filter was made and demonstrated.

Another simple example, the “if customer role in” rule is associated with a field which contains a dropdown containing the list of existing roles on WordPress. The following image illustrates the example:

  1. We have added the condition ‘if customer role’ thanks to the ‘wad_get_discounts_conditions’ filter.
  2. We added the type of operator that we want to use, it is ‘IN’ and ‘NOT IN’ thanks to the ‘wad_operators_fields_match’ filter.
  3. These are the values ​​that will be associated with our condition, we added them using the ‘wad_fields_values_match’ filter.

wad_is_rule_valid: this filter allows you to evaluate a condition before applying it or not.  For example, to check if an expression “A = B” is true before applying our reduction in price, we need the ‘wad_is_rule_valid’ filter. ‘A’ represents the condition defined by the administrator in the administration space while ‘B’ represents the second expression which will be compared with ‘A’ and therefore ‘B’ will be recovered thanks to a function that we will write ourselves and ‘A’ will be recovered by the parameter ‘$ rule’ of ‘wad_is_rule_valid’.

Last updated on March 4, 2023

Contents