0

i want to show product quantity in dropdown list ( in Configurable product ). Now i can see only title and price, but I want to see title and quantity or title price and quantity

Ex. --Chose Size -- XL - 5 items L - 2 items S - Out of Stock

1 Answer 1

1

This is a fairly straightforward thing to do. We are going to make a couple of code changes. I am assuming that you know how to create a module so I will not go through with that detail.

Background and setup:

This module needs to depend on the Mage_Catalog to ensure that your files are included in execution of the page after Mage_Catalog's files are.

Thus, to do that, you need to create your module file in app/etc/modules (with the name YourCompany_HideConfigurablePrices.xml):

<?xml version="1.0"?> <config> <modules> <YourCompany_Configurable> <active>true</active> <codePool>local</codePool> <depends> <Mage_Catalog/> </depends> </YourCompany_Configurable> </modules> </config> 

Then, create your config.xml file in app/code/local/YourCompany/HideConfigurablePrices/etc/. This is the configuration file for your module and is what tells Magento how to interact with your module. For the case of this answer, your config.xml will be super simple:

<?xml version="1.0"?> <config> <modules> <YourCompany_Configurable> <version>1.0</version> </YourCompany_Configurable> </modules> <global> <blocks> <YourCompany_Configurable> <class>YourCompany_Configurable_Block</class> </YourCompany_Configurable> <catalog> <rewrite> <product_view_type_configurable>YourCompany_Configurable_Block_Override_Product_View_Type_Configurable</product_view_type_configurable> </rewrite> </catalog> </blocks> </global> <frontend> <layout> <updates> <YourCompany_Configurable> <file>YourCompany/Configurable.xml</file> </YourCompany_Configurable> </updates> </layout> </frontend> </config> 

Creating the layout xml file:

Then, you need to create the update/layout xml file: app/design/frontend/base/default/layout/YourCompany/Configurable.xml:

<?xml version="1.0"?> <layout> <PRODUCT_TYPE_configurable translate="label" module="catalog"> <reference name="head"> <action method="addJs"><script>your-company/configurable.js</script></action> </reference> </PRODUCT_TYPE_configurable> </layout> 

Creating the override/rewrite file:

We need to modify the output of the configurable product's JSON output. Instead of copying all of the code for that function (Mage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig()), we can just take its output and work with that. This file is created at app/code/local/YourCompany/Configurable/Block/Override/Product/View/Type/Configurable.php:

class YourCompany_Configurable_Block_Override_Product_View_Type_Configurable extends Mage_Catalog_Block_Product_View_Type_Configurable { protected $_stockItems = array(); /** * @param $productId * @return Mage_CatalogInventory_Model_Stock_Item */ protected function _getStockItem($productId) { if (!isset($this->_stockItems[$productId])) { $this->_stockItems[$productId] = Mage::getModel('cataloginventory/stock_item')->load($productId, 'product_id'); } return $this->_stockItems[$productId]; } public function getJsonConfig() { $config = json_decode(parent::getJsonConfig(), true); foreach ($config['attributes'] as $attributeKey => $attributeValue) { foreach ($attributeValue['options'] as $optionKey => $optionValue) { foreach ($optionValue['products'] as $productKey => $productId) { $stockItem = $this->_getStockItem($productId); $config['attributes'][$attributeKey]['options'][$optionKey]['products'][$productKey] = array( 'id' => $productId, 'qty' => round($stockItem->getQty()) ); } } } return json_encode($config); } } 

Where the magic happens:

Finally, in js/your-company/configurable.js, we have to modify the code to create each option label. This is complicated by the fact that the product ids are not in the form that the script originally expects (an integer), and now they are an object with two keys: id and qty. We need to make these changes by monkey patching in our code:

Product.Config.prototype.fillSelect = function(element){ var attributeId = element.id.replace(/[a-z]*/, ''); var options = this.getAttributeOptions(attributeId); this.clearSelect(element); element.options[0] = new Option(this.config.chooseText, ''); var prevConfig = false; if(element.prevSetting){ prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex]; } if(options) { var index = 1; for(var i=0;i<options.length;i++){ var allowedProducts = []; if(prevConfig) { for(var j=0;j<options[i].products.length;j++){ // BEGIN CHANGE: if(prevConfig.config.allowedProducts && prevConfig.config.allowedProducts.indexOf(options[i].products[j].id)>-1){ allowedProducts.push(options[i].products[j].id); } //END CHANGE } } else { allowedProducts = options[i].products.clone(); } if(allowedProducts.size()>0){ options[i].allowedProducts = allowedProducts; // BEGIN CHANGE: element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price, this.figureQtys(allowedProducts)), options[i].id); // END CHANGE if (typeof options[i].price != 'undefined') { element.options[index].setAttribute('price', options[i].price); } element.options[index].config = options[i]; index++; } } } }; Product.Config.prototype.figureQtys = function(allowedProducts) { var minQty = Number.MAX_VALUE, maxQty = Number.MIN_VALUE; allowedProducts.forEach(function(value) { if (value.qty !== undefined) { minQty = Math.min(parseInt(value.qty)); maxQty = Math.max(parseInt(value.qty)); } }); if (minQty == maxQty) { return minQty; } else { return minQty + " - " + maxQty; } }; Product.Config.prototype.getOptionLabel = function(option, price, qty){ var price = parseFloat(price); if (this.taxConfig.includeTax) { var tax = price / (100 + this.taxConfig.defaultTax) * this.taxConfig.defaultTax; var excl = price - tax; var incl = excl*(1+(this.taxConfig.currentTax/100)); } else { var tax = price * (this.taxConfig.currentTax / 100); var excl = price; var incl = excl + tax; } if (this.taxConfig.showIncludeTax || this.taxConfig.showBothPrices) { price = incl; } else { price = excl; } var str = option.label; if(price){ if (this.taxConfig.showBothPrices) { str+= ' ' + this.formatPrice(excl, true) + ' (' + this.formatPrice(price, true) + ' ' + this.taxConfig.inclTaxTitle + ')'; } else { str+= ' ' + this.formatPrice(price, true); } } // ADDED BEGIN: if (qty !== undefined && qty > 0) { str += ' (' + qty + ' left)'; } // ADDED END return str; }; 
3
  • Hi Joseph, thank for the description. I want to know where i need to put the "Creating the override/rewrite file". I need to put the code in folder YourCompany/Configurable/Block/Override/Product/View/Type/configurable.php ? Thank you Commented Oct 4, 2015 at 11:31
  • I missed adding that. I updated the answer to show that the file is: app/code/local/YourCompany/Configurable/Block/Override/Product/View/Type/Configurable.php. Commented Oct 4, 2015 at 16:32
  • Hi Joseph, i tried but not work for me. I saw in Html than configurable.js are missing. I put the if (qty !== undefined && qty > 0) { str += ' (' + qty + ' left)'; in varien/configurable.js and not show anything ( only dropdown list with -- Select -- without any list , like color or size ). Commented Oct 6, 2015 at 19:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.