在自定义代码中添加分页以在magento中显示新添加的产品

时间:2015-01-31 05:22:16

标签: magento pagination

我可以使用自定义代码在magento中显示新添加的产品。现在我想要它的分页。这是我的代码

<?php
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect(array(
                                   'image',
                                   'name',
                                   'short_description'
                   ))
                   ->addFieldToFilter('visibility', array(
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                   )) //showing just products visible in catalog or both search and catalog
                   ->addFinalPrice()
//                        ->addAttributeToSort('price', 'asc') //in case we would like to sort products by price
                   ->getSelect()
                   //->where('price_index.final_price < price_index.price')
//                        ->limit(30) //we can specify how many products we want to show on this page
//                        ->order(new Zend_Db_Expr('RAND()')) //in case we would like to sort products randomly
                   ;

Mage::getModel('review/review')->appendSummary($_productCollection);

$_helper = $this->helper('catalog/output');
?>

我在页面http://leder.siliconbeachdev.com/index.php/new

上使用此功能

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

您可以做的是,设置集合大小和限制。在前端创建自定义分页。修改后的代码将是:

<?php 
$pageNumber = $this->getRequest()->getParam('page');
if(!$pageNumber || !is_numeric($pageNumber)){
    $pageNumber = 1;
}
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect(array(
                                   'image',
                                   'name',
                                   'short_description'
                   ))
                   ->addFieldToFilter('visibility', array(
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                   )) //showing just products visible in catalog or both search and catalog
                   ->addFinalPrice()
//                        ->addAttributeToSort('price', 'asc') //in case we would like to sort products by price
                   ->getSelect()
                   //->where('price_index.final_price < price_index.price')
//                        ->limit(30) //we can specify how many products we want to show on this page
//                        ->order(new Zend_Db_Expr('RAND()')) //in case we would like to sort products randomly
                   ;
               // Here we are showing 20 products from page 1
            $_productCollection->setPageSize(20) ->setCurPage($pageNumber);

Mage::getModel('review/review')->appendSummary($_productCollection);

$_helper = $this->helper('catalog/output');

通过这种方式,您可以在页面上进行分页。您将发送?page = pagenumber,您的控制器将通过该页面获取paage 2的20个结果并在前端显示它。