How to get only in-stock configurable option values in Magento2?

by Sukeshini   Last Updated March 26, 2018 05:09 AM

I want to get only the configurable option values which are ONLY in-stock. For instance, assume 'size' is the configurable option and it's options are XS,S,M.L,XL,XXL. Product 'test' has only S,M,L and XL products are available. XS is out of stock. But when I call the function $product->getTypeInstance()->getConfigurableOptions($product); it returns all the available sizes including XS. But I want it to be excluded.

Can someone give an idea on achieving this in the easiest manner without loading the associated products and looping through them? (because I want this value in the category page)

Any suggestions would be appreciated. I'm using Magento EE 2.0.17

Thanks.



Answers 1


You should check this method of stock Registry Magento\CatalogInventory\Model\StockRegistry $stockRegistry:

public function getChildInStock($product)
{
    $inStock = [];
    foreach ($this->getAllowProducts($product) as $_product) {
        $stockItem = $this->stockRegistry->getStockItem($_product->getId(), 1);
        $saleable = $stockItem->getIsInStock() && $this->verifyStock($stockItem);
        if($saleable){
            $inStock[] = $_product;
        }
    }
    return $inStock;
}

So you should pass configurable product and pass that product to

This will fetch all products which are intock

Naveed
Naveed
March 26, 2018 05:16 AM

Related Questions



Magento 2 change custom price frontend

Updated February 20, 2018 17:09 PM