I want to use your plugin to manage backorders. I’m syncing my woocommerce site with an external warehouse, so stock is updated automatically. I have created 2 rules:
- processing -> backorder (custom status)
- backorder -> processing
I trigger the first one if order contains a product on backorder. Order will wait in this status for replenishment. I want to trigger second one, when all products in the order have stock quantity > 0. Can you please help me to create two alg_wc_order_status_rules_do_apply_rule
filters?
filter for rule 1: do not trigger the rule if order had already been in “backorder” status. I need this condition, not to end up in an indefinite loop. I have turned on the option to record order status history in your plugin, so historical status should be somewhere in order’s meta.
filter for rule 2: I have altered your example 3 to trigger rule 2 only if all products in the order have stock higher than 0. Can you please confirm it’s ok?
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 ( 2 == $rule_id && $do_apply ) {
/**
* Now we check if any of the order's products have stock lower than 1.
*/
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
if ( $product->get_stock_quantity() < 1 ) {
/**
* We found a matching product - the rule will not be applied.
*/
return false;
}
}
/**
* We didn't find any matching products - the rule will be applied.
*/
return true;
}
return $do_apply;
}, 10, 4 );