When selling single products that are also available in bulk packages like 4 packs, 6 packs or 12 packs it may raise some inventory issues. Sure it will work well if all those products are separate and they can’t be split up, but when selling bottles of wine for example its common to be able to order a box of 6 bottles and single ones.

A common used technique within WooCommerce for such products is to use variations of a product to sell the multi-quantity packs of those products. However, the inventory issues will remain with that.

It is possible to enable inventory management on a product level, but this doesn’t deduct the proper quantity when selling a box of 6 bottles, since the customer is in fact just purchasing 1 quantity of it.ย 

In this post I’m going to show how you can in fact reduce products with a custom quantity for simple product and variations specifically.

Stock Reduction Setting

Lets start by creating a new setting first. Making it available as a simple product and variation specific setting. The following code snippet adds such setting to the products, I’ve written more about adding product settings before here; Adding Custom Product Fields.

With this in place you’ll have a setting in the ‘Inventory’ section and for variable products also a setting per variation.

Reducing Stock with Custom Quantities

Now for the part that actually reduces the custom stock quantities when someone purchases a product. The following code snippet changes the quantity that is used for reducing stock with a sum that multiplies the quantity purchased with the number that is set in our custom setting.

This takes in account when purchasing a variable product to take the variable setting, or when not set it will take the product global setting instead.

Custom Stock Quantities Reduced

As for the end result, lets take this scenario of having these product variations available;

Each variation has the Stock Reduction settings configured accordingly to 1, 6 and 12 reduction per purchased quantity of the variation. The total inventory quantity is set at the product level.

These variations will be presented accordingly on the product page;

Then when ordering a single item of each of those variations, you’ll see that it reduces the custom stock quantities accordingly from the stock level;

Thats it, hope you’ll find this useful!

Jeroen Sormani

I'm a professional WordPress plugin developer on a mission to create the best plugins for my clients. I'm specialised in developing general WordPress, WooCommerce and Easy Digital Downloads plugins.

Interested in talking about a WordPress project? Get in touch!

Follow me on Twitter

61 thoughts on “Custom Stock Quantity Reduction

Adam Davies September 27, 2018 at 1:56 pm

This is amazing and exactly what I was looking for. Should be built into Woocommerce in my opinion. Thanks again ๐Ÿ™Œ

Neville White October 16, 2018 at 6:00 pm

Hi Jeroen

Thanks very much for this very clever Custom Stock Quantity Reduction code. Greatly appreciated. I have been experimenting with it and it indeed reduces the stock as described. Do you know a way to tweak it so that unavailable quantities are hidden or marked as out of stock to prevent them being ordered? For example if there are 8 bottles left in stock, the 1 and 6 bottle options should be available but not the 12 bottle option.

Jeroen Sormani October 16, 2018 at 7:42 pm

Hi Neville,

Thats a good question.. I haven’t looked into that myself, but should be possible to do.
What I’d recommend doing is having your webdeveloper assist with that customization (feel free to reach out to me if you don’t have a developer/would like to discuss this) – I can’t really do it within this blog post.

Cheers,
Jeroen

Ricardo October 20, 2018 at 1:36 pm

Thank you man , you’re my savior ๐Ÿ˜‰

Hamid January 5, 2019 at 3:39 pm

I find it very helpful mate, Thanks for saving my time with that solution ๐Ÿ™‚

Doug Chandler January 16, 2019 at 5:33 pm

This is exactly what I am looking for…

Doug Chandler January 16, 2019 at 6:09 pm

Would love to know if you got some code snippet to check the stock levels too for each variation?
I am really surprised this is not in the base product, seems common sense to me…

Alejandra Bernal January 26, 2019 at 5:37 am

Hello Jeroen! Its exactly what i was looking for. But now i have the same problem that Neville, i need to show the items as not available to prevent people to order them.
Could you help me with that? i’ve been trying but i can’t get the right code.

Thank you!!

Dave March 11, 2019 at 11:24 am

hi

Can this snippet be made to reduce inventory of the parent SKU of the variable product rather than reducing inventory for each variant?

thanks

Jeroen Sormani March 11, 2019 at 2:30 pm

Hi Dave,

That is exactly what this blog post is describing.

Cheers,
Jeroen

Scott April 1, 2019 at 7:28 pm

Bingo!!! Genius work.

Bod April 23, 2019 at 10:07 pm

You are a life saver. Please is it possible get a code snippet that reduces purchased quantity from all products within a category. I have a shop where we sell shirts with different designs, each of these different designs are set up as separate products, so for example we have about 50 black shirts in stock, when a customer purchases 3 shirts of design A, I want all the other designs’ stock to reduce to 47pcs because in reality our black shirt stock has reduced to 47pcs. I guess All black shirts should be tied to the same category for this to work.

Please any help will be greatly appreciated.

Jeroen Sormani April 24, 2019 at 9:52 am

Hi Bod,

I don’t have something ready to go for reducing stock category wide based, but if you’d need some help I’d be happy to make my personal ask for it.
Feel free to reach out; https://jeroensormani.com/contact/

Cheers,
Jeroen

Philip Stancil May 6, 2019 at 10:19 pm

Thanks so much for sharing this, man! Should def be part of WC core.

Jason Breens May 11, 2019 at 11:33 am

Much appreciated! Quick question..Do I add an SKU for the parent inventory, and DIFFERENT SKU’s for the variations?

Jeroen Sormani May 13, 2019 at 10:34 am

SKU is not related for this snippet

Scott May 13, 2019 at 9:25 pm

Hey Jeroen, this was working great, until I updated Woo to 3.6.2. For example, items where parent stock was 500, and pack inventory value was 50 or 100, resulted in OUT OF STOCK. After much testing, I discovered that editing the parent Stock Quantity (from 500 to 501) was the only way to return to normal, expected functionality. Could this be related to code, or some kind of transient type issue?

Merissa May 26, 2019 at 7:19 am

This is AMAZING! Exactly what I needed. Thank you so so so so much for this code!

Stephanie Graham June 11, 2019 at 11:14 pm

This is exactly what I am looking for, however, I am not a developer and not comfortable with editing code. If you make this into a plug in, I could use that! It sure looks like it’s working pretty well.

Jeroen Sormani June 12, 2019 at 9:29 am

Hi Stephanie,

I wrote a blog post on adding custom code snippets here; https://aceplugins.com/how-to-add-a-code-snippet/
If you’re not comfortable doing that either you can also get it done for you.

Joel July 23, 2019 at 1:17 am

One comment on this otherwise awesome post. Is there any issues with cancelling the order, will the proper amount of product be returned upon a cancel. Had a plug-in that did this exact thing but the cancellations weren’t being returned to inventory properly.

Jeroen Sormani July 24, 2019 at 11:03 am

Good question; I haven’t tried refunds/returns yet, but I’d imagine they’d indeed not automatically be increased by the multiplier amount.
The code can of course be used to build further upon to include that as well.

Henrik July 23, 2019 at 4:15 pm

Thank you so so so so much for this code! Exacly what I needed

Scott August 9, 2019 at 5:39 pm

Hey Jeroen, have you ever looked at tackling low stock threshold for variants? For apparel shops, this is a big missing feature of Woocommerce. For example, we never keep medium shirts in same quantity as 5XL. Mediums out-sell 5XL by many multiples.

Jeroen Sormani August 12, 2019 at 11:11 am

Hi Scott,

Never looked into that – interesting thought though and shouldn’t be too difficult to do with a custom script.

Cheers,
Jeroen

Nicole August 12, 2019 at 6:07 am

I sell fruit and veg online (in quantities of single, 500g and 1kg)

This is EXACTLY what I needed – thank you so much Jeroen!

Talels October 3, 2019 at 7:30 pm

You are incredible, awesome code

Leave a Reply