Top WooCommerce & WordPress Plugins › Forums › Order Status Rules for WooCommerce › status rules used for backorders
- Support forum for the Automated Order Status Controller for WooCommerce.
- This topic has 2 replies, 3 voices, and was last updated 2 months ago by
Tom Anbinder.
- AuthorPosts
-
July 6, 2023 at 10:23 am #119728
lukasz.stachowicz
ParticipantI 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 );
July 19, 2023 at 8:37 am #120388Hi there,
Thanks for reaching out.
I have escalated this with our development team. They will get back to you as soon as they can.
Kind regards,
MoshtafizurJuly 22, 2023 at 2:12 pm #120635Hi, Lukasz,
Sorry for not getting back to you sooner.
I think this should save us from an infinite loop:
add_filter( 'alg_wc_order_status_rules_do_apply_rule', function( $do_apply, $order, $rule_id, $args ) { if ( 1 == $rule_id && $do_apply ) { if ( 'backorder' === $args['last_record']['from'] ) { return false; } } return $do_apply; }, 10, 4 );
As for your snippet for “… stock higher than 0…” – yes, it looks fine.
Please let me know what you think.
- AuthorPosts
- You must be logged in to reply to this topic.