简体   繁体   中英

Show stock of configurable products on Magento 1.9

I'm new on Magento and I have problems to show stock of configurable product. I have tried a lot of things that I found on Google, Stackoverflow, Magento Forums, but I failed. Here is the only solution that I cannot try:

$_product is your configurable product.

To get all its simple use :

$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);

So you might have something like :

foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) { $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty(); echo $simple->getName()." with size ".$simple->getSize()." have a stock of $stock"; echo '
'; } I let you adapt to your precise needs and ask question if needed

My problem is: I don't know where I can apply this solution!

屏幕截图 ()

You need to perform two step to get this working however I have checked this with version 1.8 but hope it will work in your case also

step 1. copy file app/code/core/mage/catalog/block/product/view/type/configurable.php

And create a folder under app/code/local with same directory structure mage/catalog/block/product/view/type/configurable.php

Now find function name getJsonConfig in configuration.php file.

Go to following code

  $info['options'][] = array( 'id' => $value['value_index'], 'label' => $value['label'], 'price' => $configurablePrice, 'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']), 'products' => $productsIndex, ); 

Before this code place below two lines of code

 $simpleproduct = Mage::getModel('catalog/product')->load($productsIndex); $qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); 

Here you can see that i'm getting product stock by product id, now in info option array code you have to add one parameter

 $info['options'][] = array( 'id' => $value['value_index'], 'label' => $value['label'], 'price' => $configurablePrice, 'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']), 'products' => $productsIndex, 'qty' => $qty, // we are sending stock parameter so we can use it latter in drop down field ); 

step 2. Now go js/varian/configuration.js

find the following line

  if(allowedProducts.size()>0){ options[i].allowedProducts = allowedProducts; element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id); 

Place following line under this

 element.options[index].innerHTML += " Stock is :"+options[i].qty; 

that's it all about cheer

Let me know if you have any query

with app/code/local/Mage/Catalog/Block/Product/View/Type/Configurable.php

$simpleproduct = Mage::getModel('catalog/product')->load($productsIndex); 
$qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();

                    $info['options'][] = array(
                        'id'        => $value['value_index'],
                        'label'     => $value['label'],
                        'price'     => $configurablePrice,
                        'oldPrice'  => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
                        'products'  => $productsIndex,
                        'qty'       => $qty, // we are sending stock parameter so we can use it latter in drop down field
                    );
                    $optionPrices[] = $configurablePrice;
                }
            }

and at js/varien/configurable.js

if(allowedProducts.size()>0){
                    options[i].allowedProducts = allowedProducts;
                    element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
                    if (typeof options[i].price != 'undefined') {
                        element.options[index].setAttribute('price', options[i].price);
                    }
                    element.options[index].innerHTML += " Stock is :"+options[i].qty; 
                    element.options[index].config = options[i];
                    index++;
                }

isn't working at 1.9.4... is it something wrong?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM