0

I am using magento 2.3.5 version. Even though the salable qty of the product is 0 the products still appears on the product listing page. How can we hide the product for which salable qty is 0.

I have followed below approaches.

Approach 1 :-

Created event - (event.xml)

<event name="catalog_block_product_list_collection">
    <observer name="custom_stockstatusimprovement_catalog_block_product_list_collection" instance="Custom\StockStatusImprovement\Observer\AddStockStatusToProductCollection" />
</event> 

Observer - AddStockStatusToProductCollection.php

<?php

declare(strict_types=1);

namespace Custom\StockStatusImprovement\Observer;

use Magento\Framework\{
    Event\Observer,
    Event\ObserverInterface
};
use Magento\CatalogInventory\Helper\Stock as StockHelper;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;

class AddStockStatusToProductCollection implements ObserverInterface
{
    /**
     * @var StockHelper $stockHelper
     */
    protected $stockHelper;

    private $getSalableQuantityDataBySku;


    /**
     * @param StockHelper $stockHelper
     */
    public function __construct(
        StockHelper $stockHelper,
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku
    ) {
        $this->stockHelper = $stockHelper;
        $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;


    }

    public function execute(Observer $observer) {
        
        $_productCollection = $observer->getEvent()->getCollection();

        if(count($_productCollection) > 0){
            foreach ($_productCollection as $_product) {
                $salable = $this->getSalableQuantityDataBySku->execute($_product->getSku());
                $salableQty = $salable[0]['qty'];
                
                if($salableQty <= 0){
                    $_productCollection->removeItemByKey($_product->getEntityId());
                }
            }
        }

        $observer->getEvent()->setCollection($_productCollection);

        // return $_productCollection;
    }

}

But with this approach, there is an issue with product count, filter & search.

Approach 2 :-

https://www.rakeshjesadiya.com/apply-custom-conditions-for-product-collection-in-magento-2/

Here I am not sure about how to pass salable qty logic in custom query

Can someone please guide me, how can we achieve the same.

Thanks

1 Answer 1

0

You mentioned 'Even though the salable qty of the product is 0 the products still appears on the product listing page.' - In the context of multi-stock inventory (MSI), the products' stock is not decreased after placing the order, it's only reserved until the shipment on the order is created. Your stock qty is still the same as before the order placement, whereas the saleable qty becomes 0 (if you buy the whole available qty, that is). This is why you still see your products, the display is based on the stock quantity or the real quantity, and not on the saleable quantity. As soon as the shipment is created, the saleable qty will become the same as the stock qty.

Also removing the items from the collection after the search/ navigation query was already performed will indeed leave the filter counts and collection count untouched.

So you have a few options:

  1. you index the saleable qty too and intervene before the query is performed in Elasticsearch (all search or catalog navigation/ filtering/ sorting goes through Elasticsearch) and you 'ask' for the items which have saleable qty > 0 from the start.
  2. you somehow manipulate the indexation mechanism to 'see' a product with saleable qty=0 as out of stock, which means Elasticsearch will update its documents for the products with qty=0 and you won't have to alter any collection.
  3. you add an observer to set the product as out of stock when saleable qty reaches 0. this will basically have the same result as point 2, but you will have to put the product back as in stock if its quantity is updated with a value > 0.
  4. one thing i tried on a project which managed the orders in a separate system was to create the shipment upon placing the order which means that the stock is no longer reserved, it's also decreased. but i am not sure how your business works and if you can do this. worth to mention that this is the most simple solution of all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.