我正在尝试过滤list.phtml以满足我的需要,它只根据属性的值显示产品。加载产品集合的原始代码是:
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');为了进行过滤,我得到了以下代码:
$_productCollection=$this->getLoadedProductCollection();
$cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('language', array('eq' => array('English')))
->addAttributeToSelect('*')
->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));
$_helper = $this->helper('catalog/output');这是可行的,但是分页和项目总数(从toolbar.phtml和pager.phtml生成)是不正确的。例如,原始产品集合的正确分页为7页,每页10个产品。
然而,当我使用上面显示的过滤器时,分页显示相同的7页,并且每一本过滤的书都在一页上(有18本英文书,所以18本书中有7页是重复的)。
请有人能帮我解决这个分页问题。
谢谢。
集合的SQL如下所示:
SELECT `e`.*, `at_language`.`value` AS `language`, `cat_index`.`position`
AS `cat_index_position` FROM `catalog_product_entity`
AS `e` INNER JOIN `catalog_product_entity_varchar`
AS `at_language` ON (`at_language`.`entity_id` = `e`.`entity_id`)
AND (`at_language`.`attribute_id` = '1002')
AND (`at_language`.`store_id` = 0) INNER JOIN `catalog_category_product_index`
AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1
AND cat_index.visibility IN(2, 4) AND cat_index.category_id='38'
AND cat_index.is_parent=1 WHERE (at_language.value = 'English')发布于 2013-01-29 18:54:54
我的猜测是,您弄错了产品计数,因为缺少产品可见性过滤器。尝试像这样添加它:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('language', array('eq' => array('English')))
->addAttributeToSelect('*')
->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id))
->setVisibility(
Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()
);添加:
您设置给list.phtml的自定义集合将与系统在分页器中使用的不同。分页器块Mage_Catalog_Block_Product_List_Toolbar将从没有您的过滤器的$this->_getProductCollection() (see here)获得原始产品集合。
恐怕,对模板文件中的集合进行更改是不够的。您可能需要覆盖Mage_Catalog_Block_Product_List块,特别是它的函数_getProductCollection()来实现必要的过滤。
添加2
建议对函数Mage_Catalog_Block_Product_List::_getProductCollection进行覆盖
protected function _getProductCollection()
{
$collection = parent::_getProductCollection();
$collection->addAttributeToFilter('language', array('eq' => array('English')));
$this->_productCollection = $collection;
return $this->_productCollection;
}发布于 2013-04-19 14:48:15
在您的list.phtml文件中使用以下代码:
$_productCollection->clear()
->addAttributeToFilter('attribute_set_id', array('eq' => 63))
->load();https://stackoverflow.com/questions/14580427
复制相似问题