{"id":437,"date":"2019-06-04T11:02:20","date_gmt":"2019-06-04T11:02:20","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/04\/show-magento-2-out-of-stock-associated-products-in-configurable-products-dropdown\/"},"modified":"2025-05-22T16:45:49","modified_gmt":"2025-05-22T11:15:49","slug":"show-magento-2-out-of-stock-associated-products-in-configurable-products-dropdown","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/show-magento-2-out-of-stock-associated-products-in-configurable-products-dropdown\/","title":{"rendered":"How to Show Magento 2 Out of Stock Associated Products in Configurable Products Dropdown"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A configurable product seems like a single product with drop-down lists of options for each variation. Each option is a separate simple product with a unique SKU.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The admin can track the inventory of each product variation for configurable products. However, the default Magento 2 does not bifurcate the products display in frontend based on its availability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For configurable products, what if a customer adds the option that is not in stock and he is taken to the cart only to find out that he can\u2019t make the purchase now! Isn\u2019t it the example of bad shopping experience!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution is to&nbsp;<em><strong>show Magento 2 Out of Stock associated products in configurable products dropdown<\/strong><\/em>. When associated products of the configurable products are shown under the separate dropdown along with their stock status, it makes it easy for customers to decide which products to add in cart and for which product, to subscribe for the alert notification when back in stock. To enable the notification facility, you can try the&nbsp;<em><strong><a href=\"https:\/\/meetanshi.com\/magento-2-out-of-stock-notification.html\">Magento 2 Out of Stock Notification<\/a><\/strong><\/em>&nbsp;extension!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To display the separate dropdown for out of stock associated products in configurable products, follow this method:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Show Magento 2 Out of Stock Associated Products in Configurable Products Dropdown:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: <\/strong>Create registration.php file in <strong><em>app\/code[Vendor]\/[Namespace]\/<\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n        \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n            \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n            '[Vendor]_[Namespace]',\n            __DIR__\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong> Create <strong>module.xml<\/strong> file in <strong><em>app\/code\/[Vendor]\/[Namespace]\/etc<\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n        &lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: <\/strong>Create catalog_product_view_type_configurable.xml file in <strong><em>app\/code\/[Vendor]\/[Namespace]\/view\/frontend\/layout<\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n          xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n        &lt;body>\n            &lt;referenceBlock name=\"product.info.options.wrapper\">\n                &lt;block class=\"Magento\\ConfigurableProduct\\Block\\Product\\View\\Type\\Configurable\" name=\"child-drop-down\" template=\"[Vendor]_[Namespace]::product\/child-drop-down.phtml\" before=\"-\"\/>\n            &lt;\/referenceBlock>\n        &lt;\/body>\n    &lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4: <\/strong>Create <strong>child-drop-down.phtml<\/strong> file in <strong><em>app\/code\/[Vendor]\/[Namespace]\/view\/frontend\/templates\/product<\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php $registry = $this->_helperFactory->get('Magento\\Framework\\Registry'); ?>\n        &lt;?php $_productId = $registry->registry('product')->getId() ?>\n        &lt;?php $helper = $this->helper('[Vendor]\\[Namespace]\\Helper\\Data'); ?>\n        &lt;?php $childProducts = $helper->getChildProducts($_productId);\n        if ( !empty($childProducts) ):?>\n        &lt;div class=\"out-of-stock-child-products\">\n            &lt;div class=\"col-sm-12\">\n                &lt;div class=\"col-sm-12\">\n                    &lt;label class=\"label\" for=\"product_id\" style=\"font-weight: bold\">&lt;span>&lt;?php echo __('Please select a product') ?>&lt;\/span>&lt;\/label>\n                    &lt;div class=\"control\">\n                        &lt;select name=\"product_id\" id=\"product_id\" title=\"&lt;?php echo __('Please select a product') ?>\"\n                                class=\"input-text\">\n                            &lt;option value=\"\">&lt;?php echo __('Please select a product.') ?>&lt;\/option>\n                            &lt;?php foreach ($childProducts as $key => $value): ?>\n                                &lt;option value=\"&lt;?php echo $key ?>\">&lt;?php echo $value ?>&lt;\/option>\n                            &lt;?php endforeach; ?>\n                        &lt;\/select>\n                        &lt;div class=\"product-error\"\n                             style=\"color: red; display: none\">&lt;?php echo __('Please select a product.') ?>&lt;\/div>\n                    &lt;\/div>\n                &lt;\/div>\n            &lt;\/div>\n        &lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5:<\/strong> Create <strong>Data.php<\/strong> file in <strong><em>app\/code\/[Vendor]\/[Namespace]\/Helper<\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">namespace [Vendor]\\[Namespace]\\Helper;\n    \n    use Magento\\CatalogInventory\\Model\\Stock\\Item;\n    use Magento\\Catalog\\Model\\ProductFactory;\n    \n    class Data extends AbstractHelper\n    {\n        public function __construct(Context $context, ProductFactory $productFactory, Item $stockItem)\n        {\n            parent::__construct($context);\n            $this->productFactory = $productFactory;\n            $this->stockItem = $stockItem;\n        }\n        \n        public function getChildProducts($_productId)\n        {\n            $outOfStockProducts = array();\n            try {\n                $configProduct = $this->productFactory->create()->load($_productId);\n                $childProducts = $configProduct->getTypeInstance()->getUsedProducts($configProduct);\n                foreach ($childProducts as $childProduct) {\n                    $stockItem = $this->getStockItem($childProduct->getID());\n                    if ( !$stockItem->getQty() ) {\n                        $outOfStockProducts[$childProduct->getID()] = $childProduct->getName();\n                    }\n                }\n\n            } catch (\\Exception $e) {\n                return $e->getMassage();\n            }\n            return $outOfStockProducts;\n        }\n        public function getStockItem($productId)\n        {\n            $stockItem = $this->stockItem->load($productId, 'product_id');\n            return $stockItem;\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code is compatible with Meetanshi\u2019s Magento 2 Out of Stock Notification extension.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The result is displayed in the frontend:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/05\/1_Out-of-Stock-Associated-Products-in-Configurable-Products-Dropdown-1024x470.png\" alt=\"How to Show Magento 2 Out of Stock Associated Products in Configurable Products Dropdown\" class=\"wp-image-5654\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Using this method, the customers can know the stock status of their desired configurable associated product and hence accordingly make the purchase or decide to wait!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A configurable product seems like a single product with drop-down lists of options for each variation. Each option is a separate simple product with a&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-437","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":15395,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions\/15395"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}