如果 Magento getSelectionsCollection 没有任何产品,如何以编程方式检查
How to check programmatically if Magento getSelectionsCollection don't have any product
如果在此 magento 捆绑产品列表代码中找不到任何选择产品,$selectionCollection
的输出是什么
$bundled_product_custom = new Mage_Catalog_Model_Product();
$bundled_product_custom->load($bundleParentProductId);
$selectionCollection = $bundled_product_custom->getTypeInstance(true)->getSelectionsCollection(
$bundled_product_custom->getTypeInstance(true)->getOptionsIds($bundled_product_custom), $bundled_product_custom
);
其实我需要检查这个捆绑产品是否有选择产品。
首先,您应该避免使用 new
运算符实例化对象。我建议您使用 Magento 的工厂方法,如下所示:
$bundled_product_custom = Mage::getModel('catalog/product');
这样,如果第三方扩展覆盖 Mage_Catalog_Model_Product
class,工厂方法就会根据覆盖规则实例化正确的对象。
要回答您的问题,请尝试以这种方式计算集合中的元素:
$selectionCollection->count();
// or
count($selectionCollection);
如果在此 magento 捆绑产品列表代码中找不到任何选择产品,$selectionCollection
的输出是什么
$bundled_product_custom = new Mage_Catalog_Model_Product();
$bundled_product_custom->load($bundleParentProductId);
$selectionCollection = $bundled_product_custom->getTypeInstance(true)->getSelectionsCollection(
$bundled_product_custom->getTypeInstance(true)->getOptionsIds($bundled_product_custom), $bundled_product_custom
);
其实我需要检查这个捆绑产品是否有选择产品。
首先,您应该避免使用 new
运算符实例化对象。我建议您使用 Magento 的工厂方法,如下所示:
$bundled_product_custom = Mage::getModel('catalog/product');
这样,如果第三方扩展覆盖 Mage_Catalog_Model_Product
class,工厂方法就会根据覆盖规则实例化正确的对象。
要回答您的问题,请尝试以这种方式计算集合中的元素:
$selectionCollection->count();
// or
count($selectionCollection);