【问题标题】:Magento test if a collection of products existMagento 测试是否存在产品集合
【发布时间】:2016-12-23 01:17:45
【问题描述】:
public function getAllProductById($id=0)
{
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('entity_id', array('eq' => $id))
        ->joinTable(
            'cataloginventory_stock_item',
            'product_id=entity_id',
            array('quantite' => 'qty'),
            null,
            'left'
        )
        ->load()
    ;

    if ($products) {
        return $products;
    } else {
        return $products == null;
    }
}

即使我使用未在 Magento 中设置的 id ($id),变量 $products 也不会返回 null。

【问题讨论】:

  • return $products == null;将其更改为返回 $products = null;

标签: php eclipse magento collections return


【解决方案1】:

替换你的代码

if ($products) {
        return $products;
    } else {
        return $products == null;
    }

if ($products->count()) {
        return $products;
    } else {
        return $products = null;
    }

【讨论】:

    【解决方案2】:

    只需将$products 替换为$products->count()。如果您的收藏中没有产品,这将给您零。如下所示:

    if ($products->count()) {
        return $products;
    } else {
        return $products = null;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 2014-11-14
      • 1970-01-01
      • 2011-05-29
      相关资源
      最近更新 更多