所有我试图在集合上setPageSize(),以便我可以使用它进行分页.
无论我在setPageSize中放入什么整数,我都会收到所有返回的产品.
码:
<?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 is my setcurpage and setpagesize************************/
public function allProducts() {
$collection = $this->_getCollection();
echo 'col is'.$collection->count();
/*Setting current page and 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 does not exit';}
}
public function totalPages() {
$_collections_count = $this->_getCollection()->count();
return $number_of_pages = ceil($_collections_count / $this->_itemPerPage);
}
}
Run Code Online (Sandbox Code Playgroud)
使用__constructor也是一个好主意我在这个块中使用的方式?
谢谢.
按相反顺序:
首先,永远不要将PHP __construct与Magento的块或模型一起使用,尤其是在不调用父级的情况下__construct.请改用Magento _construct.(注意下划线)
其次,你正在调用正确的分页方法.如果你简化你的代码,
$collection = Mage::getModel('catalog/product')
->getCollection();
$collection->setCurPage(1);
$collection->setPageSize(10);
$i=1;
foreach($collection as $item)
{
var_dump($i . ') ' . $item->getSku());
$i++;
}
Run Code Online (Sandbox Code Playgroud)
很容易看到setCurPage和setPageSize方法按预期工作.
//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)
Run Code Online (Sandbox Code Playgroud)
问题是你的呼唤 count
$collection = $this->_getCollection();
echo 'col is'.$collection->count();
/*Setting current page and page size */
$collection->setCurPage(1);
$collection->setPageSize($this->_itemPerPage);
return $collection;
Run Code Online (Sandbox Code Playgroud)
要计算产品集合,Magento必须加载整个集合,(加载逻辑对于简单来说太复杂了SELECT count(*)).因为您在设置页面信息之前调用了count ,所以Magento会在知道页面限制之前加载完整的集合.
您可以通过在循环之前不调用count或clear收集集合来解决此问题.尝试使用以下代码,包括和不使用$collection->clear();
$collection = Mage::getModel('catalog/product')
->getCollection();
$collection->load();
$collection->setCurPage(1);
$collection->setPageSize(10);
$collection->clear();
$i=1;
foreach($collection as $item)
{
var_dump($i . ') ' . $item->getSku());
$i++;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12918 次 |
| 最近记录: |