Magento为定制系列分页

时间:2018-03-27 15:30:26

标签: php mysql collections pagination magento-1.9

我创建了一个模块,我正在使用分页。我有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条记录分页。

1 个答案:

答案 0 :(得分:0)

方法

setPageSize()

您要设置每页分页的收藏项目数量

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

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

$this->_productCollection = $products;

return $this->_productCollection; 
  

并确保加载集合以获取适当的值。   (方法count()会这样做)