Magento - 在产品列表中为页面标题添加页码

时间:2012-03-07 15:41:30

标签: php magento

我有一个扩展程序,它使用产品列表块按属性显示产品网格过滤器(扩展名为Attribute Info Pages)。

在" _prepareLayout"此扩展程序块的功能扩展程序使用以下代码设置页面的标题和说明:

$head = $this->getLayout()->getBlock('head');
.
.
.
$head->setTitle($title);
head->setDescription($des);

我想以这种格式为标题和说明添加文字:

$page_info = "Page A of B for ";
$title = $page_info . title;
$items_info = "Listings X-Y (out of Z) ";
$des = items_info . $des;

我已尝试使用此代码来获取当前页面,最后一页,数字和项目等等:

$html_pager = Mage::getBlockSingleton('page/html_pager');
    $html_pager->setCollection($product_collection);
    $limit = Mage::getSingleton('core/app')->getRequest()->getParam('limit');
    if(empty($limit))
    {
        $limit = 8;
    }

    $html_pager->setLimit($limit);
    $LastPageNumber = $html_pager->getLastPageNum();
    $current_page = $html_pager->getCurrentPage();

    $page_info = "";
    if($current_page > 1)
    {
        $page_info = "Page " . $current_page . " of $LastPageNumber for ";
    }

    $FirstNum = $html_pager->getFirstNum();
    $LastNum = $html_pager->getLastNum();
    $TotalNum = $html_pager->getTotalNum();

    $items_info = "Listings " . $FirstNum . "-" . $LastNum . " (out of ". $TotalNum . ") ";

代码为我提供了正确的信息,但它在产品网格中引起了问题 - 它始终在产品网格中显示10个产品(无论我在&#34中选择什么;每页显示")。

如何在不破坏网格功能的情况下获取信息的任何想法?

2 个答案:

答案 0 :(得分:6)

我找到了一个适合我的解决方案:

    $product_collection = clone Mage::getSingleton('catalog/layer')->getProductCollection(); 
    $total = count($product_collection);

    $current_page = Mage::getBlockSingleton('page/html_pager')->getCurrentPage();

    $limit = Mage::getSingleton('core/app')->getRequest()->getParam('limit');
    if(empty($limit))
    {
        $limit = Mage::getStoreConfig('catalog/frontend/grid_per_page');
    }

    $pages = $total / $limit;
    $pages = ceil($pages);

    if($current_page > 1)
    {
        $page_info = "Page " . $current_page . " of $pages for ";
    }

    $FirstNum = $limit*($current_page-1)+1;

    if($current_page == $pages)
    {
        $LastNum = $total;
    }
    else
    {
        $LastNum = $limit + ($FirstNum - 1);
    }

    $items_info = "Listings " . $FirstNum . "-" . $LastNum . " (out of ". $total . ") ";

答案 1 :(得分:0)

$pager = Mage::getBlockSingleton('page/html_pager');
$productCollection = Mage::getSingleton('catalog/layer')->getProductCollection();
$pager->setCollection($productCollection);

之后,你已经获得了所有的块方法(Mage_Page_Block_Html_Pager):

$pager->getCurrentPage();
$pager->getLastPageNum();