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

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

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


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);
            $inStock[] = $_product;
    return $inStock;

So you should pass configurable product and pass that product to

This will fetch all products which are intock

March 26, 2018 05:16 AM

Related Questions

Hierarchical Bundled Products

Updated February 01, 2019 16:09 PM

Configurable product own url M2

Updated March 18, 2019 11:09 AM