Skip to content

Out of stock product visible on storefront. After updating the product programmatically

I’m working on integration, where I’m updating a product stock. But Whenever I set product stock to zero the product gets out-of-stock(as intended) from the backend but is still visible on the storefront.
Code used for product/stock update

if ($quantity > 0) {
     $prod->setStockData(array('use_config_manage_stock' => 0,
                               'manage_stock' => 1,
                               'is_in_stock' => 1,
                               'qty' => $quantity
                                ));
} else {
     $prod->setStockData(array('use_config_manage_stock' => 0,
                               'manage_stock' => 1,
                               'is_in_stock' => 0,
                               'qty' => 0
                                ));
}
$prodmodel->save();

The Indexer is set to Update on save.
display out-of-stock product is set to No from store config.

P.S
Whenever I do reindexing everything works perfectly.
one option is to reindexing the after every product save but we’ve thousands of products and this can be very inefficient.
I can’t share the full code.
enter image description here


enter image description here


enter image description here


Any kind of information is appreciated. Thanks!