How to add custom conditions?

It is possible to add the custom conditions by using "rp_wcec_custom_conditions" filter.

But note that this should be integrated into the structure of the default conditions. You can find the default array of conditions in on_init() method, in file woocommerce-email-center\includes\classes\rp-wcec-conditions.class.php

So, for example (below), we will try to add new condition for Order Items - we will need to integrate it into "order_products" group, and in "children" we will add new element with the id of our new condition - in this case "order_product_wccf_meta". Then we add a "label", and set "method" as one of default values (needed for correctly rendering the UI).

Other things can be used as default, but we also need to set "callback" as the name of the function that will check the condition value. And at the end we can merge new condition with the default array of custom conditions - just to provide the compatibility with other code.

add_filter('rp_wcec_custom_conditions', 'wcec_add_custom_conditions'); 
function wcec_add_custom_conditions($conditions) {
    
    $new_conditions = array(
        'order_products' => array(
            'children' => array(
                'order_product_wccf_meta' => array(
                    'label'         => __('WCCF - Product field value', 'rp_wcec'),
                    'method'        => 'matches_does_not_match',
                    'uses_fields'   => array('text'),
                    'schedule_type' => array('conditions'),
                    'context'       => array(
                        'trigger' => array(
                            'woocommerce_order' => true,
                        ),
                    ),
                    'callback'      => 'wcec_wccf_match_product_field',
                ),
            ),
        ),
    );

    return array_merge($conditions, $new_conditions);
}

And then we will need to create the callback function. It can be anything, but in all cases, will need to use the 2 parameters provided there:

  • the $condition array: it has the method to compare and the value from the UI to compare - in $condition['text'].
  • and $args array, which has a lot of other things, from which the most important is the order object - in $args['order'].

In this example we get the comparable value, entered in the UI, and the actual value, extracted from the order items meta (in this case it's actually from our Custom Fields plugin):

function wcec_wccf_match_product_field($condition, $args) {

    // Get the variables
    $method = $condition[$condition['type'] . '_method'];
    $condition_value = $condition['text'];
    $order = $args['order'];
    $field_key = 'example';

    // Iterate order items
    $order_items = $order->get_items();

    foreach ($order_items as $item_id => $item) {

        $field_value = wccf_get_values(array(
            'key'          => $field_key,
            'context'      => 'product_field',
            'item_id'      => $item_id,
            'include_meta' => false
        ));

        if ($method === 'matches') {
            return $condition_value == $field_value;

        }
        else if ($method === 'does_not_match') {
            return $condition_value != $field_value;
        }
    }
}
Have more questions? Submit a request