status rules used for backorders

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #119728
    lukasz.stachowicz
    Participant

    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:

    1. processing -> backorder (custom status)
    2. 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 );
    #120388
    Moshtafizur
    Moderator
    Plugin Support

    Hi 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,
    Moshtafizur

    #120635
    Tom Anbinder
    Moderator
    Plugin Support

    Hi, 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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.