Product inventory

Tips and tricks how you can manage your WooCommerce stores product stock programmatically.

Create a Custom Stock Status

In this tutorial I am going to show you how you can create a custom stock status for your WooCommerce products. There are a bunch of plugins over there, which allow you to implement similar functionality in one way or another, but why to use a plugin when can do exactly the same with a couple of lines of code in your theme. But of course, it depends.

As an example here we will create a “Contact us” custom product stock status, which will be kind of similar to “Out of stock” – customers won’t be able to add a product to a cart, but there is going to be a link to a contact page instead of an “Add to cart” button.

This custom product stock status will work with both standard WooCommerce features (like product filters) and third party plugins (my Duplicate SKU stock sync plugin for example).

Duplicate SKU Stock Sync

Sync stock between products and product variations with the same SKU automatically within a single WooCommerce store.

Simple Inventory Sync

This WooCommerce stock synchronisation plugin allows you to sync the stock quantity of the products (and variations) with the same SKU between different stores.

Bulk Update Product Stock Quantities

In the previous tutorial we already discussed updating WooCommerce product stock quantities programmatically when we covered how to do it with REST API specifically.

The whole idea can be deconstructed into two simple things:

  1. Connect to woocommerce_product_set_stock (or woocommerce_variation_set_stock action hooks for product variations).
  2. Send a REST API request to /wc/v3/products/{$product_id} (or /wc/v3/products/{$product_id}/variations/{$variation_id} for product variations).

That’s pretty much it. But there is a “but”. If a specific WooCommerce order in your store can contains a lot of products, then you have a problem. For example, there are 50 products in a single order, then guess what? 50 REST API requests are going to be sent in order to sync all the products stock quantities.

So there is no other way than to update the stock quantities in bulk. But how to do it?

Multiple Products from the Same Stock

In this tutorial I will show you how you make both Stock Quantity and Stock Status to be automatically shared between different products on your WooCommerce store. But if you come to this tutorial looking for a product inventory synchronisation tool between different WooCommerce stores, then please take a look at my plugin.

Also I am going to show you an example with variations of the same product.

The whole idea is pretty simple and can be breaked down into two steps:

  1. Creating a custom function connected either to the woocommerce_product_set_stock or to the woocommerce_variation_set_stock action hook if we talking about variations.
  2. Obtaining the connected products and to setting the same stock values for them.

Easy peasy.

Update Products and Variations Stock from CSV

In this tutorial I would like to share with you a ready to use example how you can sync WooCommerce products and variations inventory with a CSV file automatically.

Our CSV file is going to look like this:

sync WooCommerce product stock quantities with CSV files
Of course, if you’re going to use the file just to sync stock quantities, ID and Name columns better be removed.

To make this tutorial a little bit simpler we are going to put the file in the WordPress uploads folder, but for sure you can create a WooCommerce settings page where you can upload it.

Reduce Product Stock Quantities Conditionally

When you turn the option “Manage Stock” on for your WooCommerce products (or variations), then every time an order is made, stock quantity of the products from the order is going to be automatically decreased.

But sometimes you don’t need that, or just need it for specific order statuses or payment methods. In this tutorial I will show you how you can do that.