简体   繁体   English

Magento分页用于自定义收藏

[英]Magento pagination for custom collection

I have created a module in which i am using pagination.I have 901 products which satisfy my filtered collection but i want to get only 360 products. 我创建了一个使用分页的模块。我有901个满足筛选后的产品的产品,但我只想获得360个产品。 see below code for more understanding.. 请参阅下面的代码以了解更多信息。

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;               
    }
    }

but when i go to front end it show me pagination according to 901 products. 但是当我转到前端时,它会根据901产品向我显示分页。 when I echo $this->_productCollection->getSize() it shows me 901 records but when I use $this->_productCollection->count() it shows me 360 records. 当我回显$ this-> _ productCollection-> getSize()时,它显示了901条记录,但当我使用$ this-> _ productCollection-> count()时,它显示了360条记录。 how can I get pagination for only 360 records for my custom filtered collection. 如何为我的自定义过滤集合仅获得360条记录的分页。

with method 与方法

setPageSize()

you are setting the amount of collection items per one page of pagination 您正在设置每页分页中的收集项目数量

if you want to limit your collection to 360 items: 如果要将收藏品限制为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) (方法count()可以)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM