Snippet

Automatically Reduce Stock Level After Order in WooCommerce

How to automatically reduce stock in woocommerceWoocommerce reduce stock after order placedAutomatically update stock levels woocommerceReduce stock on pending payment woocommerceWoocommerce stock management automationAutomatically decrease inventory after orderWoocommerce stock reduction codePrevent overselling by managing stock woocommerceWoocommerce reduce stock on order status changeAutomate stock reduction in wordpress

Explanation

This code snippet is designed to help you automatically manage your WooCommerce store's inventory by reducing stock levels whenever an order is placed. This helps prevent overselling and keeps your inventory accurate.

How It Works:

  • When an order is placed and its status changes to "processing," "completed," or "on-hold," the code kicks in.
  • It checks each item in the order to see if the product's stock is being managed.
  • If stock management is enabled for the product, the code calculates the new stock level by subtracting the ordered quantity from the current stock.
  • The product's stock quantity is then updated to reflect the new amount.

Note: This code is triggered by specific order statuses, ensuring that stock is only reduced when an order is confirmed or being processed, thus preventing premature stock reduction.

Code

1<?php 2/** 3 * Automatically reduce stock level in WooCommerce after an order is placed. 4 * 5 * This function hooks into the WooCommerce order status change to reduce stock levels 6 * when an order is placed, preventing overselling and ensuring accurate inventory management. 7 * 8 * @param int $order_id The ID of the order. 9 */ 10function wp_dudecom_reduce_stock_on_order_status_change( $order_id ) { 11 // Get the order object 12 $order = wc_get_order( $order_id ); 13 14 // Check if the order is valid 15 if ( ! $order ) { 16 return; 17 } 18 19 // Loop through each item in the order 20 foreach ( $order->get_items() as $item_id => $item ) { 21 // Get the product object 22 $product = $item->get_product(); 23 24 // Check if the product manages stock 25 if ( $product && $product->managing_stock() ) { 26 // Get the current stock quantity 27 $current_stock = $product->get_stock_quantity(); 28 29 // Calculate the new stock quantity 30 $new_stock = $current_stock - $item->get_quantity(); 31 32 // Update the product stock quantity 33 wc_update_product_stock( $product, $new_stock ); 34 } 35 } 36} 37add_action( 'woocommerce_order_status_processing', 'wp_dudecom_reduce_stock_on_order_status_change', 10, 1 ); 38add_action( 'woocommerce_order_status_completed', 'wp_dudecom_reduce_stock_on_order_status_change', 10, 1 ); 39add_action( 'woocommerce_order_status_on-hold', 'wp_dudecom_reduce_stock_on_order_status_change', 10, 1 ); 40?>

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.
  • Products should have stock management enabled.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are adding the code to functions.php. Alternatively, navigate to Plugins > Editor if you are using a custom plugin.
  3. Locate and open the functions.php file or your custom plugin file.
  4. Copy the provided code snippet.
  5. Paste the code at the end of the functions.php file or your custom plugin file.
  6. Click Update File to save your changes.
  7. Test the functionality by placing a test order and checking if the stock levels are reduced accordingly.

If you need help with implementation or more advanced functionality, consider using the services of wp-dude.com.