cancel
Showing results for 
Search instead for 
Did you mean: 

POSTing a virtual product doesn't set stock status quite right - any way around?

POSTing a virtual product doesn't set stock status quite right - any way around?

Using a REST API to POST a product of type_id "virtual", and there are a couple of issues.

1. Doing a POST request for a virtual product and even though a product's JSON has

"stock_item" : 	{ "is_in_stock" : false,
		"qty" : 0 }

and is subsequently shown as "out of stock" with quantity "0" in Admin, it's still shown in the storefront as available, but you cannot actually add it to the cart, a message immediately appears saying that required quantity is not available (which is of course expected). Got me puzzled for a while, but it's fixed by running php bin/magento indexer:reindex.

2. After reindexing everything appears as it should (only in-stock options are visible), however when I change quantities in our database and POST the same virtual product again, the stock status would only change if going from "0/out of stock" to 1 or more. Then the Front immediately picks it up and I see the option appear. However, when a certain virtual product already was in-stock with quantity > 0 and I'm doing a POST to mark it as out of stock with quantity = 0, it's still visible in the storefront until I do a reindex.

 

Note: if all of the virtual products are listed set as out of stock then even after POSTing the main product is not shown, as expected. But if even a single virtual product has quantity > 0 then all of the options/virtual products are visible inside the main product.

 

Questions, therefore:

1. Am I doing something wrong? Why, even though I explicitly say that a virtual product has 0 quantity, it still appears in the store until I do a reindex? Why does it work fine the other way without reindexing?

2. If it's indeed a feature, is it possible to make a REST request to trigger a reindex instead of going to the CLI to do it?

 

3 REPLIES 3

Re: POSTing a virtual product doesn't set stock status quite right - any way around?

Something is not right with the indexer, but I cannot figure out why it's behaving like that. Currently I can POST/PUT a virtual product with a quantity of 0 to make the entire product disappear from the store as expected, however re-POSTing the same virtual product with quantity > 0 doesn't bring it back and even deleting the product in Admin and re-POSTing won't do! The procedure that works for those stuck products is: delete all related products in Admin, do a reindex, re-POST the product.

I just can't understand why it's getting stuck. Even if I do a reindex (and cache clean/flush) after the product quantity is > 0 in Admin, it changes nothing, product is still not being displayed.

Re: POSTing a virtual product doesn't set stock status quite right - any way around?

What is reindexing doing exactly? Is it related to MySQL indexes or is it related to Elasticsearch?

Re: POSTing a virtual product doesn't set stock status quite right - any way around?

Restoring a MySQL dump (with a showing product) alone doesn't help, but restoring said dump, deleting Elasticsearch index and reindexing does work. It's way too hard to debug what's going on.