无法获取给定类别的产品集合
Failing to fetch product collection for a given category
我已经编写了一个方法,可以从我的 list.phtml 文件中调用。
该逻辑应该简单地输出当前查看类别中的产品数量以及最高和最低价格。真的很直接。
出于我无法理解的原因,它总是返回当前类别不包含任何产品,我不知道为什么,但我认为这是因为我没有正确获取产品集合。
这是我的方法的代码。
public function writeCatInfo(){
if(Mage::registry('current_category')){
$_productCollection=$this->getLoadedProductCollection;
$catname = Mage::registry('current_category')->getName();
$priceArray = array();
foreach ($_productCollection as $_product){
$priceArray[] = $_product->getSpecialPrice();
}
$numItems = count($_productCollection);
$highPrice = number_format(max($priceArray),2);
$lowPrice = number_format(min($priceArray),2);
$infostring = ucwords($catname)." contains ".$numItems." products with prices ranging from £".$lowPrice." to £".$highPrice;
echo $infostring;
}
}
这将帮助您获取当前类别的产品集合。
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($category)
->setOrder('price', 'ASC')
->load();
原来我的助手扩展了错误的 class - Mage_Core_Helper_Abtract
而不是 Mage_Catalog_Block_Product_List
。更改此设置可解决问题。
请参阅以下代码以按类别 id 获取产品集合
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // 添加所有属性 - 可选
->addAttributeToFilter('status', 1) // 启用
->addAttributeToFilter('visibility', 4) //目录可见性,搜索
->setOrder('price', 'ASC'); //按价格设置订单
我已经编写了一个方法,可以从我的 list.phtml 文件中调用。 该逻辑应该简单地输出当前查看类别中的产品数量以及最高和最低价格。真的很直接。
出于我无法理解的原因,它总是返回当前类别不包含任何产品,我不知道为什么,但我认为这是因为我没有正确获取产品集合。
这是我的方法的代码。
public function writeCatInfo(){
if(Mage::registry('current_category')){
$_productCollection=$this->getLoadedProductCollection;
$catname = Mage::registry('current_category')->getName();
$priceArray = array();
foreach ($_productCollection as $_product){
$priceArray[] = $_product->getSpecialPrice();
}
$numItems = count($_productCollection);
$highPrice = number_format(max($priceArray),2);
$lowPrice = number_format(min($priceArray),2);
$infostring = ucwords($catname)." contains ".$numItems." products with prices ranging from £".$lowPrice." to £".$highPrice;
echo $infostring;
}
}
这将帮助您获取当前类别的产品集合。
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($category)
->setOrder('price', 'ASC')
->load();
原来我的助手扩展了错误的 class - Mage_Core_Helper_Abtract
而不是 Mage_Catalog_Block_Product_List
。更改此设置可解决问题。
请参阅以下代码以按类别 id 获取产品集合
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // 添加所有属性 - 可选
->addAttributeToFilter('status', 1) // 启用
->addAttributeToFilter('visibility', 4) //目录可见性,搜索
->setOrder('price', 'ASC'); //按价格设置订单