如何在产品页面magento1.9中同时显示简单产品和可配置产品的缺货状态

时间:2019-06-26 04:26:16

标签: magento-1.9

我有使用1.9版本进行开发的magento网站,我需要在简单产品和可配置产品的产品页面上显示缺货状态

我尝试了下面的方法,它只是获得简单产品的状态

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
  $qty = $stock->getQty();

我需要的是如何在产品页面中显示简单和可配置产品的缺货状态

1 个答案:

答案 0 :(得分:0)

此代码对我有用,
希望对您有帮助。

if( $_product->getTypeId() == 'simple' ){
    if ($_product->getStockItem()->getIsInStock()<= 0) { ?>
    <span class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></span>
    <?php }else { ?>
    <span class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></span>
    <?php }; 
}
else{

    $_product->getTypeInstance(true)->getUsedProducts ( null, $_product);
    foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();    
        $stockTotal = $stockTotal + $stock;
    }

    if ($stockTotal <= 0){ ?>
    <span class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></span>
    <?php } else{ ?>
    <span class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></span>
    <?php } 

}