magento - how implement setPagesize for pagination, right now returning all products -
all trying setpagesize() on collection can use pagination.
i getting products returned no matter integer put setpagesize.
code:
<?php class rik_featured_block_featured extends mage_core_block_template { private $_itemperpage = 2; public $_category_id = '' ; private $_currentpage = ''; public function __construct() { $custom_var_code = mage::getmodel('core/variable')->loadbycode('homepage_firstrow_prod'); if(isset($custom_var_code)) echo $this->_category_id = $custom_var_code->getvalue('text') ; echo $page_var = $this->getrequest()->getparam('p'); if(isset($page_var)&&$page_var!=0){ $this->_currentpage = $page_var; } if(isset($page_var)&&$page_var == '0'){ $this->_currentpage = '1'; } } /****************** here setcurpage , setpagesize************************/ public function allproducts() { $collection = $this->_getcollection(); echo 'col is'.$collection->count(); /*setting current page , page size */ $collection->setcurpage(1); $collection->setpagesize($this->_itemperpage); return $collection; } private function _getcollection() { $category = mage::getmodel('catalog/category')->load($this->_category_id); if($category->geturlpath() != null) { $collection = $category->getproductcollection(); //mage::helper('catalog/product')->setskipsaleablecheck(true); $collection->addwebsitefilter(); $collection->addurlrewrite($this->_category_id); $collection->addminimalprice()->addfinalprice()->addtaxpercents(); mage::getsingleton('catalog/product_visibility') ->addvisibleincatalogfiltertocollection($collection); mage::getsingleton('catalog/product_status') ->addvisiblefiltertocollection($collection); $collection->addattributetoselect(array('entity_id', 'sku', 'name', 'short_description', 'description', 'price', 'thumbnail', 'image', 'url_path', 'type_of')); return $collection; } else { echo 'cat not exit';} } public function totalpages() { $_collections_count = $this->_getcollection()->count(); return $number_of_pages = ceil($_collections_count / $this->_itemperpage); } }
also idea use __constructor way using in block?
thank you.
in reverse order:
first, never user php's __construct
magento's blocks or models, without calling parent __construct
. use magento's _construct
instead. (note underlines)
second, you're calling correct pagination methods. if simplify code little,
$collection = mage::getmodel('catalog/product') ->getcollection(); $collection->setcurpage(1); $collection->setpagesize(10); $i=1; foreach($collection $item) { var_dump($i . ') ' . $item->getsku()); $i++; }
it's easy see setcurpage
, setpagesize
methods work expected.
//results of running above code string '1) n2610' (length=8) string '2) bb8100' (length=9) string '3) sw810i' (length=9) string '4) 8525pda' (length=10) string '5) mm-a900m' (length=11) string '6) ma464ll/a' (length=12) string '7) lx.fr206.001' (length=15) string '8) vgn-txn27n/b' (length=15) string '9) m285-e' (length=9) string '10) cn_3' (length=8)
the problem call count
$collection = $this->_getcollection(); echo 'col is'.$collection->count(); /*setting current page , page size */ $collection->setcurpage(1); $collection->setpagesize($this->_itemperpage); return $collection;
to count product collection, magento must load entire collection, (the loading logic complicated simple select count(*)
). because you're calling count before set page information, magento loads full collection before it's aware of page restrictions.
you can fix not calling count
, or clear
ing collection before looping. try following code, both , without $collection->clear();
$collection = mage::getmodel('catalog/product') ->getcollection(); $collection->load(); $collection->setcurpage(1); $collection->setpagesize(10); $collection->clear(); $i=1; foreach($collection $item) { var_dump($i . ') ' . $item->getsku()); $i++; }
Comments
Post a Comment