Automatically Reduce Stock Level After Order in WooCommerce

How to automatically reduce stock in woocommerce; Woocommerce reduce stock after order placed; Automatically update stock levels woocommerce; Reduce stock on pending payment woocommerce; Woocommerce stock management automation; Automatically decrease inventory after order; Woocommerce stock reduction code; Prevent overselling by managing stock woocommerce; Woocommerce reduce stock on order status change; Automate 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

<?php
/**
 * Automatically reduce stock level in WooCommerce after an order is placed.
 *
 * This function hooks into the WooCommerce order status change to reduce stock levels
 * when an order is placed, preventing overselling and ensuring accurate inventory management.
 *
 * @param int $order_id The ID of the order.
 */
function wp_dudecom_reduce_stock_on_order_status_change( $order_id ) {
    // Get the order object
    $order = wc_get_order( $order_id );

    // Check if the order is valid
    if ( ! $order ) {
        return;
    }

    // Loop through each item in the order
    foreach ( $order->get_items() as $item_id => $item ) {
        // Get the product object
        $product = $item->get_product();

        // Check if the product manages stock
        if ( $product && $product->managing_stock() ) {
            // Get the current stock quantity
            $current_stock = $product->get_stock_quantity();

            // Calculate the new stock quantity
            $new_stock = $current_stock - $item->get_quantity();

            // Update the product stock quantity
            wc_update_product_stock( $product, $new_stock );
        }
    }
}
add_action( 'woocommerce_order_status_processing', 'wp_dudecom_reduce_stock_on_order_status_change', 10, 1 );
add_action( 'woocommerce_order_status_completed', 'wp_dudecom_reduce_stock_on_order_status_change', 10, 1 );
add_action( 'woocommerce_order_status_on-hold', 'wp_dudecom_reduce_stock_on_order_status_change', 10, 1 );
?>

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.