Support » Plugin: WooCommerce » ‘Pending payment’ order status (not reducing stock)

  • Resolved fcacic

    (@fcacic)


    Is there a way for Woocommerce to reduce stock of a product while the order is in ‘pending payment’ status?

    • This topic was modified 8 months, 4 weeks ago by fcacic.
    • This topic was modified 8 months, 4 weeks ago by fcacic.
    • This topic was modified 8 months, 4 weeks ago by fcacic.
    • This topic was modified 8 months, 4 weeks ago by fcacic.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Nicola Mustone – a11n

    (@nicolamustone)

    Automattic Happiness Engineer

    Hello there,
    You can do that by adding a new action in your functions.php code:

    
    add_action( 'woocommerce_order_status_pending', 'wc_maybe_reduce_stock_levels' );
    

    This should work, as seen in the original code here: https://github.com/woocommerce/woocommerce/blob/02cf0dfaed5923513de0c88add597d1560c2cfd2/includes/wc-stock-functions.php#L108

    Please, notice that this might lead to wrong stock info since the reduction happens before the customer paid for the product. If the payment fails the stock will still be reduced.

    Plugin Support laceyrod a11n

    (@laceyrod)

    Automattic Happiness Engineer

    Hey @fcacic,

    I hope that code snippet did the trick for you! This thread has been inactive for a bit, so I’m going to mark it as Resolved now. Please feel free to open a new one if you have any further questions.

    Cheers!

    mirror79

    (@mirror79)

    Hi, I have absolutely same situation as @fcacic and the snippet mentioned above is not working. Did you test in on the latest version of wordpress and woocommerce? It probably stopped working due to some update. I need to reduce the stock in all new orders, which has been created in “pending” status, because the customer choosed to pay by any payment gateway like PayPal etc. Could you please test or provide another solution? Thank you very much. M.Hr.

    • This reply was modified 8 months ago by mirror79.
    mirror79

    (@mirror79)

    Well I found solution. This is the only snippet working for me:

    
    add_action( 'woocommerce_checkout_create_order', 'force_new_order_status', 20, 1 );
    function force_new_order_status( $order ) {
    
        if( ! $order->has_status('on-hold') )
            $order->set_status( 'on-hold', 'Forced status by a custom script' );
    }
    

    But it is not putting back the stock when payment failed. It will have to be fixed using another hook.

    manugerino

    (@manugerino)

    @mirror79 I’ve tried your code, and it almost works, but when I try to end the payment process, the website says that is item is already out of stock and does not let e finish the order.
    I think that it should remove the stock after the order is finished, but it is removing it before the process ends.
    Do you know what can I do?

    add_action( ‘woocommerce_checkout_create_order’, ‘force_new_order_status’, 20, 1 );
    function force_new_order_status( $order ) {

    if( ! $order->has_status(‘on-hold’) )
    $order->set_status( ‘on-hold’, ‘Forced status by a custom script’ );

    If payment is not received and canceled, does not replace stock

    • This reply was modified 6 months, 3 weeks ago by umutdur12.

    Anything new on this one? The problem described by @manugerino is pretty nasty. The last stock item can’t be bought because the stock gets reduced before the customer can even pay…

    • This reply was modified 3 months, 2 weeks ago by naroha.

    >> Is there a way for Woocommerce to reduce stock of a product while the order is in ‘pending payment’ status?

    Hi @fcacic,
    How did you resolve this issue? I am facing the same problem and don’t know how to correct it. Just waiting for woocommerce to resolve this issue in their upcoming update.

    • This reply was modified 2 months, 2 weeks ago by rajhandi88.

    Hi, yes, it is more complicated, then it seems above. It is much more complex.

    I made two steps to make it working correctly:

    1. you have to control the _reduced_stock (search web how it works) – like manually deleting this flag when order is cancelled etc.

    2. you have to move the force switching to on-hold (mentioned above) into the source code of payments (like PayPal, etc.)

    The complete guide would take several screens and a lot of time. If you really want to, I will do it, just let me know, if you are interested. But beware: it will not be only about Snippets, but even the source code of payment gates must be edited (which means that if you will update it – like PayPal, you have to edit the code again) – I made my own app which automatically overwrite part of a code after every update … so if you are interested even in this kind of high level editing, let me know.

    • This reply was modified 2 months, 2 weeks ago by mirror79.
    • This reply was modified 2 months, 2 weeks ago by mirror79.
    • This reply was modified 2 months, 2 weeks ago by mirror79.
    • This reply was modified 2 months, 2 weeks ago by mirror79.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.