首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento - list.phtml过滤产品集合未提供正确的分页

Magento - list.phtml过滤产品集合未提供正确的分页
EN

Stack Overflow用户
提问于 2013-01-29 18:13:26
回答 2查看 14.2K关注 0票数 2

我正在尝试过滤list.phtml以满足我的需要,它只根据属性的值显示产品。加载产品集合的原始代码是:

代码语言:javascript
复制
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');

为了进行过滤,我得到了以下代码:

代码语言:javascript
复制
$_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如下所示:

代码语言:javascript
复制
 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')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-29 18:54:54

我的猜测是,您弄错了产品计数,因为缺少产品可见性过滤器。尝试像这样添加它:

代码语言:javascript
复制
$_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进行覆盖

代码语言:javascript
复制
protected function _getProductCollection()
{ 
    $collection = parent::_getProductCollection();
    $collection->addAttributeToFilter('language', array('eq' => array('English')));
    $this->_productCollection = $collection;

    return $this->_productCollection;
}
票数 4
EN

Stack Overflow用户

发布于 2013-04-19 14:48:15

在您的list.phtml文件中使用以下代码:

代码语言:javascript
复制
$_productCollection->clear()
    ->addAttributeToFilter('attribute_set_id', array('eq' => 63))
    ->load();
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14580427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档