Sarathlal N

How to Update Product Quantity in WooCommerce Using Custom Code

Managing inventory is essential for any online store. WooCommerce, a popular e-commerce platform, allows you to update product quantities manually. However, there are times when you might need to update these quantities automatically through custom code. This guide will show you how to update product quantities in WooCommerce programmatically using simple, custom code.

Why Update Product Quantities Programmatically?

Updating product quantities programmatically can be beneficial in several scenarios, such as:

Using WooCommerce Hooks and Functions

WooCommerce provides hooks and functions that make it easy to interact with product data. In this tutorial, we will use the woocommerce_thankyou hook to update product quantities when an order is placed.

Step-by-Step Guide

1. Adding Custom Code to Your Child Theme’s functions.php

To update product quantities programmatically, you need to add custom code to your child theme’s functions.php file. Here is an example:

// Function to update product stock quantity
function update_product_stock_quantity($product_id, $quantity) {
    // Get the product object
    $product = wc_get_product($product_id);
    
    // Check if the product exists
    if ($product) {
        // Update the stock quantity
        $product->set_stock_quantity($quantity);
        
        // Save the product to update the stock quantity in the database
        $product->save();
        
        // Optionally, you can set the stock status to 'instock' or 'outofstock'
        // $product->set_stock_status('instock');
        // $product->save();
    }
}

// Hook into WooCommerce to update product stock quantity when an order is placed
add_action('woocommerce_thankyou', 'custom_update_product_stock_on_order');

function custom_update_product_stock_on_order($order_id) {
    // Get the order object
    $order = wc_get_order($order_id);
    
    // Loop through the order items
    foreach ($order->get_items() as $item_id => $item) {
        // Get the product ID from the order item
        $product_id = $item->get_product_id();
        
        // Set the new quantity (example: setting it to 100)
        $new_quantity = 100;
        
        // Update the product stock quantity
        update_product_stock_quantity($product_id, $new_quantity);
    }
}

2. Explanation of the Code

3. Customization

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.