Nightly Script to Set Negative Quantity Items to Zero Quantity

rstrazz
New Member
3 0 0

I'm looking for some advice on where to start here.  Basically, we sell items in our store that can be out of stock and as a result the stock quantity will go into negative values.  So we will have something with a -1 or -2 quantity for inventory.   All I want to do is write a simple script that will look nightly for anything that is less than zero quantity and set it to zero.  I want it to ignore anything that is zero or more quantity, just looking for negatives and overwriting to zero.

 

I have some coding background, but looking for some general guidance on the approach.  As fas as where to run this script, I could run it on Google or AWS nightly, etc.  Just trying to figure out the most simple way of doing this, I'm open to various options and not married to the approach.

 

Thank you! :)

0 Likes
rstrazz
New Member
3 0 0

Hoping for a little guidance, it seems that I could use GraphQL to run something like this:

 

products(query:inventory_total<0) {
    title,
    description
}

 

But instead of the title and description, I'd want to modify this to be retrieving IDs of the products.  And then I would save up that list of product IDs and issue a mutation with each one to set the inventory to 0?  We are not dealing with multiple locations here either, just one value for stock.  Again just looking for some general guidance on approach to hopefully save some time spinning my wheels.  Thanks!

0 Likes
FabienLG
Tourist
14 0 2

Hello,

 

Indeed, that's basically what I would do, get all the products and for each product, fire a mutation.

Be careful that products results are paginated and avoid triggering too many mutations at once to avoid hitting Shopify's API rate limits.

International team available for Store creation and Theme/App development!
0 Likes
rstrazz
New Member
3 0 0

Just wanted to follow up here I ended up being able to solve this much, much more easily by detecting a negative value in liquid code in my theme "product-form.liquid" and displaying zero.

Just in case anyone else runs into this same scenario, here's what I did.  (Note that I'm super new to liquid code and I have tested this myself and it worked but shouldn't be considered a best practice)

New Code

{% if current_variant.available and current_variant.inventory_management %}
    	{% if current_variant.inventory_quantity < 0 %}
      		{{ 'products.product.stock_label' | t: count: 0 }}
     	{% else %}
     		{{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }}
   	{% endif %}
{% endif %}

Old Code

{% if current_variant.available and current_variant.inventory_management %}     
   {{ 'products.product.stock_label' | t: count: 0 }}
{% endif %}
0 Likes