【问题标题】:How to check if Configurable Product is out of stock?如何检查可配置产品是否缺货?
【发布时间】:2014-10-08 23:49:27
【问题描述】:

我们都知道,magento 中的可配置产品与简单产品相关联。

如果与可配置产品关联的简单产品变为Inventory = 0,则表示可配置产品缺货

所以问题是我如何检测可配置产品是否缺货?我想检测,所以我可以在前端显示“缺货”文本。

类似的东西

if($configurable_product->isOutOfStock()) {
   echo "Out of Stock";
}

我如何在 Magento 中做到这一点?

【问题讨论】:

    标签: magento inventory configurable-product


    【解决方案1】:
    if (!$configurable->isSaleable() ||$configurable_product->getIsInStock()==0){
    // out of stock
    }
    

    用于检查子简单产品:

    $allProducts = $configurable->getTypeInstance(true)
                    ->getUsedProducts(null, $configurable);
                foreach ($allProducts as $product) {
                    if (!$product->isSaleable()|| $product->getIsInStock()==0) {
                        //out of stock for check child simple product
                    }
                }
    

    【讨论】:

    • 您的第一个代码对我不起作用。但是您的第二个代码有效,因此即使速度较慢,我也使用了,因为我需要循环与可配置产品关联的所有产品
    【解决方案2】:
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
    $qty = $stockItem->getData('qty');
    $inStock = $stockItem->getData('is_in_stock');
    
    if ($qty < 1 || $inStock == 0) {
        // OutOfStock
    }
    

    我更喜欢仔细检查数量,因为根据配置设置,数量 == 0 的产品并不总是缺货。

    【讨论】:

      【解决方案3】:
      $_productCollection = Mage::getResourceModel('catalog/product_collection')
      ->addAttributeToFilter('type_id', array('eq' => 'configurable'));
      
      Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);
      

      这仅显示库存的可配置产品。

      【讨论】:

        【解决方案4】:

        对 Quovadisqc 的回答进行了轻微的更新/更正。定义 $qty 时应该是

        $qty = $stockItem->getData('qty'); // correct
        

        而不是当前存在的,

        $qty = $stockItem->setData('qty'); // incorrect
        

        我会将此作为评论发布,但我没有足够的代表。

        【讨论】:

          【解决方案5】:

          在产品的 foreach 循环中,以下 if 语句有效。

          if ($product->getIsInStock() === '1' && $product->isSaleable() === true) {
              echo 'this product is in stock';
          }
          

          【讨论】:

            猜你喜欢
            • 2015-04-02
            • 2013-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-16
            • 2011-09-01
            • 2021-12-04
            相关资源
            最近更新 更多