You can add any custom rule condition with the alg_wc_order_status_rules_do_apply_rule
filter.
Example #1: Check if the customer had any previous orders
add_filter( 'alg_wc_order_status_rules_do_apply_rule', function( $do_apply, $order, $rule_id, $args ) { /** * First we are checking: * a) if it's the rule we need, * b) if the rule's status, etc. conditions are fulfilled, * c) if it's not a guest's order. */ if ( 1 == $rule_id && $do_apply && ( $user_id = $order->get_user_id() ) ) { /** * Now we check if the current user had any previous orders. * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query */ $_orders = wc_get_orders( array( 'customer_id' => $user_id, // from the same user only 'date_created' => '<' . $order->get_date_created(), // older orders only 'limit' => 1, // one order is enough ) ); /** * Finally we set the rule to be applied only to orders from new customers. * If we'd need to apply the rule only for old customers instead, we'd use: * return ( ! empty( $_orders ) ); */ return ( empty( $_orders ) ); } return $do_apply; }, 10, 4 );
Example #2: Check if the customer has purchased this product in any of his previous orders
add_filter( 'alg_wc_order_status_rules_do_apply_rule', function( $do_apply, $order, $rule_id, $args ) { /** * First we are checking: * a) if it's the rule we need, * b) if the rule's status, etc. conditions are fulfilled, * c) if it's not a guest's order. */ if ( 1 == $rule_id && $do_apply && ( $user_id = $order->get_user_id() ) ) { /** * Now we get all user's previous orders. * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query */ $_orders = wc_get_orders( array( 'customer_id' => $user_id, // from the same user only 'status' => array( 'wc-completed' ), // completed orders only 'date_created' => '<' . $order->get_date_created(), // older orders only 'limit' => -1, // no limit ) ); /** * Go through all the products in the current order. */ foreach ( $order->get_items() as $item ) { $product_id = ( ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'] ); /** * Go through all the other orders. */ foreach ( $_orders as $_order ) { /** * Go through all the products in each order. */ foreach ( $_order->get_items() as $_item ) { $_product_id = ( ! empty( $_item['variation_id'] ) ? $_item['variation_id'] : $_item['product_id'] ); if ( $product_id === $_product_id ) { /** * We found a match - the rule can be applied. */ return true; } } } } /** * We didn't find a match - the rule will not be applied. */ return false; } return $do_apply; }, 10, 4 );
Example #3: Check if the order has any products with stock quantity lower than or equal to 5
add_filter( 'alg_wc_order_status_rules_do_apply_rule', function( $do_apply, $order, $rule_id, $args ) { /** * First we are checking: * a) if it's the rule we need, * b) if the rule's status, etc. conditions are fulfilled. */ if ( 1 == $rule_id && $do_apply ) { /** * Now we check if any of the order's products have stock lower than or equal to 5. */ foreach ( $order->get_items() as $item ) { $product = $item->get_product(); if ( $product->get_stock_quantity() <= 5 ) { /** * We found a matching product - the rule will be applied. */ return true; } } /** * We didn't find any matching products - the rule will not be applied. */ return false; } return $do_apply; }, 10, 4 );