[英]Magento - list.phtml filtering product collection not giving correct pagination
我正在嘗試過濾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')
在list.phtml文件中使用它:
$_productCollection->clear()
->addAttributeToFilter('attribute_set_id', array('eq' => 63))
->load();
我的猜測是您錯誤地計算了產品數量,因為缺少產品可見性過濾器。 嘗試像這樣添加:
$_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
的自定義集合與系統在分頁器中使用的自定義集合不同。 paginator塊Mage_Catalog_Block_Product_List_Toolbar
將從$this->_getProductCollection()
( 參見此處 )獲取原始產品集合, $this->_getProductCollection()
集合沒有您的過濾器。
我擔心,對模板文件中的集合進行更改是不夠的。 您可能必須覆蓋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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.