gpt4 book ai didi

php - 用于自定义集合的 Magento 分页

转载 作者:行者123 更新时间:2023-11-30 21:45:52 25 4
gpt4 key购买 nike

我创建了一个使用分页的模块。我有 901 种产品可以满足我过滤后的收藏,但我只想获得 360 种产品。请参阅下面的代码以获得更多理解..

class Test_Module_Block_Listmore extends Mage_Catalog_Block_Product_List 
{
public function __construct()
{
parent::__construct();
$collection = $this->_getProductCollection();
$this->setCollection($collection);

}

public function _getProductCollection()
{

$time = time();
$lastTime = $time - 7776000; // 90*60*24*60
$from = date('Y-m-d', $lastTime);
$to = date('Y-m-d', $time);
$storeId = Mage::app()->getStore()->getId();
$attribute_code = "country_code";
$attribute_details = Mage::getSingleton("eav/config")->getAttribute("catalog_product", $attribute_code);
$options = $attribute_details->getSource()->getAllOptions(false);
foreach($options as $option)
{
$value=$option["value"];
$label=$option["label"];
if($label=='SG')
{
$sg=$option["value"];
}
if($label=='MY')
{
$my=$option["value"];
}
}

$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId($storeId)
->addAttributeToSelect('*')
->addAttributeToFilter(array(array('attribute'=>'country_code',array('in'=> array($sg,$my)))))
->addAttributeToFilter('publication_date', array('lteq' => date("Y-m-d 00:00:00") ))
->addAttributeToFilter('gr_date', array('lteq' => date("Y-m-d 00:00:00") ))
->addAttributeToSort('publication_date', 'DESC');

$categoryWise = Mage::app()->getRequest()->getParam('category_id');
if($categoryWise){

$_category = Mage::getModel('catalog/category')->load($categoryWise);
if($_category->getChildren()){

$products->getSelect()
->join(array('category'=>"catalog_category_product"),"e.entity_id=category.product_id")
->where("category.category_id = '".$_category->getId()."' or category.category_id in (".$_category->getChildren().")");
}else{

$products->getSelect()
->join(array('category'=>"catalog_category_product"),"e.entity_id=category.product_id")
->where("category.category_id = '".$_category->getId()."'");
}

$products->getSelect()->group('e.entity_id');

}


$products->setPageSize(360);

$this->_productCollection = $products;

return $this->_productCollection;
}
}

但是当我转到前端时,它会根据 901 产品向我显示分页。当我回显 $this->_productCollection->getSize() 时,它显示了 901 条记录,但是当我使用 $this->_productCollection->count() 时,它显示了 360 条记录。如何为我的自定义过滤集合获取仅 360 条记录的分页。

最佳答案

用方法

setPageSize()

你正在设置每一页分页的收藏品数量

如果您想将收藏限制为 360 个项目:

$products->getSelect()->limit(360);

$this->_productCollection = $products;

return $this->_productCollection;

And make sure to load the collection to get appropriate values. (method count() will do)

关于php - 用于自定义集合的 Magento 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com