在Magento 1.9上显示可配置产品的库存 [英] Show stock of configurable products on Magento 1.9
问题描述
我是Magento的新手,我无法显示可配置产品的库存.我尝试了很多我在Google,Stackoverflow,Magento论坛上发现的东西,但是失败了.这是我无法尝试的唯一解决方案:
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是您可配置的产品.
$_product is your configurable product.
要获得所有简单的使用方法,请执行以下操作:
To get all its simple use :
$ _ product-> getTypeInstance(true)-> getUsedProducts(null,$ _product);
$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);
所以您可能会遇到类似这样的事情:
So you might have something like :
foreach($ _product-> getTypeInstance(true)-> getUsedProducts(null,
$ _product)为$ simple){
$ stock = Mage :: getModel('cataloginventory/stock_item')-> loadByProduct($ simple)-> getQty();
echo $ simple-> getName().,大小为".$ simple-> getSize().,有$ stock的股票";
回声'
'; }我让您适应自己的确切需求,并询问是否需要
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!
()
推荐答案
您需要执行两个步骤才能使此工作正常运行,但是我已经在1.8版中进行了检查,但希望它也能在您的情况下正常工作
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
步骤1.复制文件app/code/core/mage/catalog/block/product/view/type/configurable.php
step 1. copy file app/code/core/mage/catalog/block/product/view/type/configurable.php
并在app/code/local下创建一个具有相同目录结构的文件夹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
现在在configuration.php文件中找到函数名称getJsonConfig.
Now find function name getJsonConfig in configuration.php file.
转到以下代码
$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();
在这里您可以看到我正在按产品ID获取产品库存,现在在信息选项数组代码中,您必须添加一个参数
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
);
第2步.现在进入js/varian/configuration.js
step 2. Now go js/varian/configuration.js
找到以下行
if(allowedProducts.size()>0){
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
将以下行置于此
element.options[index].innerHTML += " Stock is :"+options[i].qty;
仅此而已
让我知道您是否有任何疑问
Let me know if you have any query
这篇关于在Magento 1.9上显示可配置产品的库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!