閱讀575 返回首頁    go 技術社區[雲棲]


magento -- 可配置產品頁獲得所屬簡單產品的庫存

想在產品頁顯示可配置產品所屬的簡單產品的庫存,解決方案如下:

 

打開文件mage/catalog/block/product/view/type/configurable.php,大概在85行可以找到如下代碼:

 

foreach ($this->getAllowAttributes() as $attribute) { $productAttribute = $attribute->getProductAttribute(); $attributeValue = $product->getData($productAttribute->getAttributeCode()); if (!isset($options[$productAttribute->getId()])) { $options[$productAttribute->getId()] = array(); } if (!isset($options[$productAttribute->getId()][$attributeValue])) { $options[$productAttribute->getId()][$attributeValue] = array(); } $options[$productAttribute->getId()][$attributeValue][] = $productId; } 

 

替換成

 

foreach ($this->getAllowAttributes() as $attribute) { $productAttribute = $attribute->getProductAttribute(); $attributeValue = $product->getData($productAttribute->getAttributeCode()); if (!isset($options[$productAttribute->getId()])) { $options[$productAttribute->getId()] = array(); } if (!isset($options[$productAttribute->getId()][$attributeValue])) { $options[$productAttribute->getId()][$attributeValue] = array(); } $options[$productAttribute->getId()][$attributeValue][] = $productId; //Alex $options['qty'][$product -> getAttributeText($productAttribute->getName())] = floor($product->getStockItem()->getQty()); } 

 

然後在大概128行找到如下代碼:

 

$info['options'][] = array( 'id' => $value['value_index'], 'label' => $value['label'] , 'price' => $this->_preparePrice($value['pricing_value'], $value['is_percent']), 'products' => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(), ); 

 

替換成

 

$info['options'][] = array( 'id' => $value['value_index'], 'label' => ($options['qty'][$value['label']] <= 0) ? $value['label'] . ' * out of stock' : $value['label'] . " * (".$options['qty'][$value['label']]." in stock)", 'price' => $this->_preparePrice($value['pricing_value'], $value['is_percent']), 'products' => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(), ); 

 

然後前台通過一點JS處理下就可以得到類似淘寶的效果

淘寶

最後更新:2017-04-02 04:26:02

  上一篇:go HTTP請求發送XML數據
  下一篇:go 使用jxl對excel寫文件簡單例子