• Resolved baconallday

    (@baconallday)


    add_filter( 'woocommerce_can_reduce_order_stock', 'can_reduce_order_stock_exceptions', 9000, 2 );
    function can_reduce_order_stock_exceptions( $can_reduce, $order ) {
        // Define your product ID(s) or variation ID(s) that will avoid stock reduction
        $targeted_ids = array( 796 );
    
        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {
            if ( in_array($item->get_product_id(), $targeted_ids) 
            || in_array($item->get_variation_id(), $targeted_ids) ) {
                $can_reduce = false;
                break;
            }
        }
        return $can_reduce;
    }

    If I create an order with any products + the product with id 796 and change the order status to completed it still reduces my product stock. I tried adding debug statements and it seems the function only gets called after creating the order and then changing the status to ‘Processing’.

    But when I change the order status to ‘Completed’ it still reduces my stock. (No appearance in debug.log)

    Is there a different filter I have to use for that?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @baconallday,

    It looks like the function can_reduce_order_stock_exceptions is set to run when the woocommerce_can_reduce_order_stock filter is triggered. This filter is typically triggered when an order status changes to Processing.

    However, your issue is that when the order status changes to Completed, the stock is still being reduced. This is because the woocommerce_can_reduce_order_stock filter does not get triggered when the order status changes to Completed.

    To resolve this, you can use the woocommerce_order_status_completed action hook. This hook gets triggered when an order status changes to Completed. You can then call your function within this hook to prevent stock reduction.

    Please note that writing or providing custom code is not within the scope of our support policy. If you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter baconallday

    (@baconallday)

    Hi @shameemreza,

    thank you so much. I wrote some logic, that automatically increases the stock for the same amount when woocommerce_order_status_completed is called.

    Have a awesome day!

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @baconallday,

    I’m glad you were able to find a solution to your inquiry here! ??

    Should you have further inquiries, kindly create a new topic here.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Don’t reduce stock on order status change to completed’ is closed to new replies.