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

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. Dynamically Generating a Table of Contents in WordPress
  3. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  4. Understanding the Impact of git reset --hard Command
  5. How to Move Uncommitted Changes to a New Branch in Git

Your Questions / Comments

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