{"id":81,"date":"2016-01-19T21:36:05","date_gmt":"2016-01-19T21:36:05","guid":{"rendered":"http:\/\/magebrew.com\/blog\/?p=81"},"modified":"2016-01-19T21:36:05","modified_gmt":"2016-01-19T21:36:05","slug":"show-out-of-stock-options-on-product-page-in-magento","status":"publish","type":"post","link":"https:\/\/magebrew.com\/blog\/show-out-of-stock-options-on-product-page-in-magento\/","title":{"rendered":"Show out of stock options on product page in Magento"},"content":{"rendered":"<p>By default in Magento if simple product is out of stock it will not be shown on configurable product page as option. This article will help you to show\u00a0out of stock options on product page.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-82 size-full\" src=\"http:\/\/magebrew.com\/blog\/wp-content\/uploads\/2016\/01\/out-of-stock-options-in-magento.png\" alt=\"Out of stock options in Magento\" width=\"920\" height=\"382\" srcset=\"https:\/\/magebrew.com\/blog\/wp-content\/uploads\/2016\/01\/out-of-stock-options-in-magento.png 920w, https:\/\/magebrew.com\/blog\/wp-content\/uploads\/2016\/01\/out-of-stock-options-in-magento-300x125.png 300w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p><!--more--><\/p>\n<p>First create module skeleton with\u00a0<a href=\"https:\/\/github.com\/netz98\/n98-magerun\" target=\"_blank\">n98-magerun<\/a> tool:<\/p>\n<pre class=\"lang:sh decode:true \">n98-magerun.phar dev:module:create Magebrew Options<\/pre>\n<p>Now we need to rewrite\u00a0<em>Mage_Catalog_Block_Product_View_Type_Configurable<\/em> class to adjust options JSON config that is being passed to template. We are going to override\u00a0<em>getAllowProducts<\/em> and\u00a0<em>getJsonConfig<\/em> methods. Here is how it should looks like:<\/p>\n<pre class=\"lang:php decode:true \">#file app\/code\/local\/Magebrew\/Options\/Block\/Catalog\/Product\/View\/Type\/Configurable.php\r\nclass Magebrew_Options_Block_Catalog_Product_View_Type_Configurable extends Mage_Catalog_Block_Product_View_Type_Configurable\r\n{\r\n    \/**\r\n     * Get Allowed Products\r\n     *\r\n     * @return array\r\n     *\/\r\n    public function getAllowProducts()\r\n    {\r\n        if (!$this-&gt;hasAllowProducts()) {\r\n            $skipSaleableCheck = Mage::helper('catalog\/product')-&gt;getSkipSaleableCheck();\r\n            Mage::helper('catalog\/product')-&gt;setSkipSaleableCheck(true);\r\n            parent::getAllowProducts();\r\n            Mage::helper('catalog\/product')-&gt;setSkipSaleableCheck($skipSaleableCheck);\r\n        }\r\n        return $this-&gt;getData('allow_products');\r\n    }\r\n\r\n    \/**\r\n     * Composes configuration for js\r\n     * overridden to support Out Of Stock labels\r\n     * @return string\r\n     *\/\r\n    public function getJsonConfig()\r\n    {\r\n        $configEncoded = parent::getJsonConfig();\r\n        $config = Mage::helper('core')-&gt;jsonDecode($configEncoded);\r\n        $unSaleable = array();\r\n        foreach ($this-&gt;getAllowProducts() as $product) {\r\n            if (!$product-&gt;isSaleable()) {\r\n                $unSaleable[] = $product-&gt;getId();\r\n            }\r\n        }\r\n        $config['unsaleable'] = $unSaleable;\r\n        $config = Mage::helper('core')-&gt;jsonEncode($config);\r\n\r\n        return $config;\r\n    }\r\n}<\/pre>\n<p>Also we need to edit template file that is responsible for options rendering. It is\u00a0<em>catalog\/product\/view\/type\/options\/configurable.phtml<\/em>. If there is no such file in you current theme copy it from base theme (<em>app\/design\/frontend\/base\/default\/template\/catalog\/product\/view\/type\/options\/configurable.phtml<\/em>). All we need to do here is to\u00a0override JavaScript\u00a0<em>getOptionLabel<\/em> method. Add the following code to the <em>&lt;script&gt;<\/em> section\u00a0of the template:<\/p>\n<pre class=\"lang:js decode:true \">        \/\/overriding getOptionLabel method to add Out Of Stock label\r\n        Product.Config.addMethods({\r\n            getOptionLabel: function(option, price){\r\n                var price = parseFloat(price);\r\n                if (this.taxConfig.includeTax) {\r\n                    var tax = price \/ (100 + this.taxConfig.defaultTax) * this.taxConfig.defaultTax;\r\n                    var excl = price - tax;\r\n                    var incl = excl*(1+(this.taxConfig.currentTax\/100));\r\n                } else {\r\n                    var tax = price * (this.taxConfig.currentTax \/ 100);\r\n                    var excl = price;\r\n                    var incl = excl + tax;\r\n                }\r\n\r\n                if (this.taxConfig.showIncludeTax || this.taxConfig.showBothPrices) {\r\n                    price = incl;\r\n                } else {\r\n                    price = excl;\r\n                }\r\n\r\n                var str = option.label;\r\n                if (option.allowedProducts.length == 1){\r\n                    if (this.config.unsaleable){\r\n                        if (this.config.unsaleable.indexOf(option.allowedProducts[0]) &gt; -1){\r\n                            str += ' (out of stock)';\r\n                        }\r\n                    }\r\n                }\r\n                if(price){\r\n                    if (this.taxConfig.showBothPrices) {\r\n                        str+= ' ' + this.formatPrice(excl, true) + ' (' + this.formatPrice(price, true) + ' ' + this.taxConfig.inclTaxTitle + ')';\r\n                    } else {\r\n                        str+= ' ' + this.formatPrice(price, true);\r\n                    }\r\n                }\r\n                return str;\r\n            }\r\n        });\r\n<\/pre>\n<p>Refresh cache and you should see Out Of Stock options on product page. Check complete code on <a href=\"https:\/\/github.com\/magebrew\/out-of-stock-options-magento\/\" target=\"_blank\">GitHub<\/a> if it doesn&#8217;t work for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default in Magento if simple product is out of stock it will not be shown on configurable product page as option. This article will help you to show\u00a0out of stock options on product page.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[15,3,13,14],"_links":{"self":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/81"}],"collection":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":8,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}