Knowing when a product is out of stock via Product API

jfi
New Member
1 0 0

I am making a Product API call to a store and this is the response json that I get:

 

{
   "id"   =>4409000000097,
   "title"   =>   "Product Title",
   "updated_at"   =>   "2020-01-05T11:49:26-06:00",
   "body_html"   =>   "Native to XXXXXX.",
   "handle"   =>   "product-title",
   "image"   =>   "https://cdn.shopify.com/s/files/1/xxxxx.jpg?v=1576999667",
   "variants"   =>[
      {
         "id"         =>31400027000000,
         "inventory_policy"         =>         "deny",
         "inventory_quantity"         => 0,
         "price"         =>         "41.95",
         "sku"         =>         "",
         "title"         =>         "Default Title",
         "option1"         =>         "Default Title",
         "option2"         =>nil,
         "option3"         =>nil,
         "image_id"         =>nil
      }
   ],
   "options"   =>[
      #<ShopifyAPI::      Option:      0x0000556500000008 @attributes=      {
         "id"         =>5727200000003,
         "product_id"         =>4409000000097,
         "name"         =>         "Title",
         "position"         =>1,
         "values"         =>[
            "Default Title"
         ]
      },
      @prefix_options=      {

      },
      @persisted=true>
   ],
   "images"   =>[
      {
         "id"         =>13711000000045,
         "src"         =>         "https://cdn.shopify.com/s/files/1/gggggg.jpg?v=1579999967",
         "variant_ids"         =>[

         ]
      }
   ]
}

According to the Product Variant API reference

https://help.shopify.com/en/api/reference/products/product-variant

 

 "inventory_policy" => "deny",
 "inventory_quantity" => 0,

this means that the product should be out of stock. But on the store website this product is still available for purchase!?

 

Could you let me know how can I know if the product is available for purchase on the store website by making a Product API call?

 

Thanks

 

0 Likes
hassain
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 136

Hey @jfi ,

 

If the inventory_quantity is 0 and the inventory_policy is set to deny, then customers should not be able to purchase the item. Could you provide the Shopify store's myshopify.com URL where you are seeing this issue and the Product Variant's name? It may be something with the store's theme that chooses to display that this product variable is available for sale still, but when a customer tries to checkout an error will be thrown since the product is out of stock.

 

Alternatively if you use the GraphQL Admin API instead of REST to query for Products + Product Variants that way, there is a boolean field called "availableForSale" that returns True or False if the item is available for sale or not: https://help.shopify.com/en/api/graphql-admin-api/reference/object/productvariant#fields-2019-10

 

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
mctenold
Shopify Partner
42 0 5

I'm having the same issue here as well. Is there no way to get product availability via the REST API?

 

@hassain with a product with no variants that has no inventory management, product.available in liquid still shows it being available, yet in the REST API inventory_quantity is 0 and inventory_policy is 'deny'.

 

This means as far as I can tell, there is no way via REST api to check whether or not a product is available.

0 Likes
mctenold
Shopify Partner
42 0 5

Bump.

0 Likes
mctenold
Shopify Partner
42 0 5

Bump.

0 Likes
hassain
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 136

Hi @mctenold ,

 

It is not possible to have a product with no variants. By default, all products will have at least 1 variant defined. If you have not specified a variant during product creation, a variant called "Default Variant" will be created and assigned to your product.

 

Through the Shopify Admin REST API, you will be able to see that this Default Variant will have inventory_management defined, inventory_policy defined, and inventory_quantity defined. If inventory_quantity is 0, and inventory_policy is set to 'deny', then you know this product should no longer be sold.

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
mctenold
Shopify Partner
42 0 5

@hassain 

 

There must be some discrepancy.

 

Using this logic:

 

Through the Shopify Admin REST API, you will be able to see that this Default Variant will have inventory_management defined, inventory_policy defined, and inventory_quantity defined. If inventory_quantity is 0, and inventory_policy is set to 'deny', then you know this product should no longer be sold.

I'm getting that a product is out of stock.

 

But on the product page, using this logic:

{% if product.available %}

I'm getting that that same product is available.

 

Can you clarify?

0 Likes
hassain
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 136

Hey @mctenold ,

 

That is interesting. Based on the definition for Liquid property of 'product.available' it states that "Returns falseif all of the products variants' inventory_quantity values are zero or less, and their inventory_policy is not set to "Allow users to purchase this item, even if it is no longer in stock."

 

So if you are seeing that through the Admin API the inventory_quantity is zero and the policy is set to deny purchasing if it is out of stock, but the liquid property is still returning `True`, then something must be wrong.

 

In order for me to investigate more, could you provide me with the myshopify.com URL of the store in question, and the Product ID? Feel free to DM me this information if you are not comfortable with sharing it in this thread

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
mctenold
Shopify Partner
42 0 5

@hassain 

 

Basically I'm getting the same thing that OP is describing in their original message:

 

According to the Product Variant API reference

https://help.shopify.com/en/api/reference/products/product-variant

 

 "inventory_policy" => "deny",
 "inventory_quantity" => 0,

this means that the product should be out of stock. But on the store website this product is still available for purchase!?

 

Does a check of inventory_management need to be considered anywhere?

 

Basically my product, on all variants, has inventory_quantity = 0, inventory_policy = "deny", and inventory_management = null, yet product.available still returns true.

 

I'll PM you my store/product info.

0 Likes
hassain
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 136

Hey @mctenold ,

 

Thank you for sharing that additional information with me through the DMs. It does appear that 'inventory_management: null' is the culprit here.

 

If you look through the Admin for the store in question, you will see that this product is explicitly configured to "Inventory Not Tracked". Because this product is set to "Inventory Not Tracked" (inventory_management=null), it means that Shopify ignores any and all fields related to inventory. So inventory quantity and inventory policy are ignored and meaningless if the product is set to not have its inventory tracked, and the product will always look like its available for sale.

 

To resolve this, update the product's variants in the Shopify Admin so that its inventory is tracked, either by Shopify or some custom fulfillment service. 

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes