Konstantinos E.
Konstantinos E.

Reputation: 155

WooCommerce Order Status & reduce Stock

i write a payment gateway for woocommerce and i change the order status after payment is success. If status change to processing i reduce the order stock. Everything goes well.

By the way in the plugin settings the admin can change the status for success payments... processing or completed (for physical or virtual products)

    function setOrderPaid($OrderID, $status){
        $order = new WC_Order($OrderID);    
        if(!$order){
            return false;
            }else{
                    $order->update_status($status);                     
                    if($status=="processing") $order->reduce_order_stock();
                    WC()->cart->empty_cart();                           
                    return true;
                }
    }

After a success payment with changing the order status to processing, i go and change the status manual to completed (backoffice woocommerce->orders) and the system reduce the order stock again.

I have to reduce the stock after the success payment, to prevent problems with other orders on the same product. How can i fix this order reduce problem?

I find this Woocommerce set_status. Maybe this helps... bool $manual_update is this a manual order status change? So the system knows that stock is already reduced???

Upvotes: 1

Views: 3544

Answers (1)

Gaurav
Gaurav

Reputation: 367

This has to do with what are considered as paid order statuses in woocommerce. You can customize the list of paid order statuses. The above function will trigger each time the order enters any of those paid order statuses.

Now, your first instinct would be to truncate the paid status list. This may be advisable in a few cases, but in this particular case the instinct is wrong.

What you need to do instead is check the current order status. If the status is not a paid status, only then you should trigger the reduce stock function. This assumes that the order won't jump between a paid and unpaid status repeatedly.

Upvotes: 0

Related Questions