【发布时间】:2011-06-16 04:09:32
【问题描述】:
我不太了解 Magento 集合的工作原理,所以希望这是一个简单的问题...
正如古老的格言所说,你不能观察一个实验而不以某种方式改变它。这似乎适用于 Magento 集合。我有一个我编写的特色产品 模块,效果很好。我们最近向我们的商店添加了客户评论。查看类别页面时,它会显示对该类别中产品的随机评论。这也很好用。我将评论块添加到我的特色产品页面,这很容易做到,但由于这些产品不属于特定类别,它只是随机抽取一个通常不相关的评论。为了解决这个问题,我在我的 Featured 模块中修改了我的 getProductCollection 函数,并在集合创建/保存后将以下内容添加到末尾:
$_product = $this->_productCollection->getFirstItem();
$_catIDs = $_product->getCategoryIds();
if(count($_catIDs) > 0)
{
$_cat = Mage::getModel('catalog/category')->load($_catIDs[0]);
Mage::register('current_category', $_cat);
}
不幸的是,仅仅查看集合中的第一个项目的行为就会破坏工具栏中的寻呼机。无论我选择哪种分页选项,当上述代码到位时,它总是显示所有项。如果我注释掉该部分,它就可以正常工作。
所以我的问题是:如何在不以某种方式更改集合或破坏分页的情况下获取有关集合中产品的任何信息?
添加我的代码以帮助更多地解释问题:
class VPS_Featured_Block_List extends Amasty_Sorting_Block_Catalog_Product_List//Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection))
{
$_attributeNames = 'featured';
if($this->getAttributeName() != '')
$_attributeNames = $this->getAttributeName();
$_attrArray = explode(',', $_attributeNames);
$this->_productCollection = Mage::getModel('catalog/product')->getCollection();
$this->_productCollection->addAttributeToSelect('*');
$_filters = array();
foreach($_attrArray as $_attr)
$_filters[] = array('attribute' => $_attr, 'eq' => true);
$this->_productCollection->addFieldToFilter($_filters);
//$this->_productCollection->addFieldToFilter(array(array('attribute' => $_attr, 'eq' => true),));
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);
//Get category of first product in collection (used for featured review)
$_catIDs = array();
$_product = $this->_productCollection->getFirstItem();
$_catIDs = $_product->getCategoryIds();
if(count($_catIDs) > 0)
{
$_cat = Mage::getModel('catalog/category')->load($_catIDs[0]);
Mage::register('current_category', $_cat);
}
}
return $this->_productCollection;
}
}
【问题讨论】:
标签: magento