Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Automatically Reduce Stock for Specific Order Status in WooCommerce?

By default, WooCommerce reduces the product stock quantity when the order status is set to ‘processing’ or ‘on-hold’. This means that when a customer places an order and it transitions to one of these statuses, WooCommerce automatically deducts the purchased quantity from the available stock. But here’s the twist: what if you prefer to update your stock when the order is ‘completed’?

Solution: Reduce Stock for Specific WooCommerce Order Status

The code snippet will help you to reduce stock quantity of products whenever the status of an order is changed to ‘completed’.

add_filter( 'woocommerce_can_reduce_order_stock', 'ts_no_stock_changes_for_cod', 25, 2 );
function ts_no_stock_changes_for_cod( $reduce_stock, $order ) {

	if( $order->has_status( 'processing' ) && 'cod' === $order->get_payment_method() ) {
		$reduce_stock = false;
	}
	return $reduce_stock;

}

add_action( 'woocommerce_order_status_changed', 'ts_reduce_stock_completed_orders', 20, 4 );
function ts_reduce_stock_completed_orders( $order_id, $old_status, $new_status, $order ){

	// do nothing if this order was not not with cash on delivery payment method
	if( 'cod' !== $order->get_payment_method() ) {
		return;
	}

	// do nothing if it is not changing order status to "completed"
	if ( 'completed' !== $new_status ){
		return;
	}

	// reduce stock levels manually
	wc_reduce_stock_levels( $order_id );

}


Output

When an order is placed, the code checks if the orders are in the “processing” state and the payment method is “cash on delivery.” If so the stock of those order items will not be reduced.

Once the orders made with “cash on delivery” are marked as “completed,” the stock levels are reduced.


How to Automatically Reduce Stock for Specific Order Status in WooCommerce? - Tyche Softwares


As you can take control over the quantity using order statuses, you can also control the statuses getting changed only on certain timeframes. Check out this post on how to automatically change order status only within certain timeframes.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x