dtjbcda841554 2014-06-26 07:07 采纳率: 0%
浏览 44

如何在magento的子类别集合中添加分页?

I have written following code to display subcategories

    <ul class="">
  <?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
$categorycount = 0;
foreach ($_categories as $_category):
if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
?>
  <li class="">
    <div class="">
      <div class="">
        <div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="">
          <?php
                    $imageUrl = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/".$this->getCurrentCategory()->getThumbnail();

                    $imageResized = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/"."resize/".$this->getCurrentCategory()->getThumbnail();


                    if (!file_exists($imageResized) && file_exists($imageUrl)) 
                    {

                    $imageObj = new Varien_Image($imageUrl);
                    $imageObj->constrainOnly(TRUE);
                    $imageObj->keepAspectRatio(TRUE);
                    $imageObj->keepFrame(FALSE);
                    $imageObj->quality(100);
                    $imageObj->resize(270, 270);
                    $imageObj->save($imageResized);
                    }
                    ?>
          <span class=""><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/resize/'.$this->getCurrentCategory()->getThumbnail(); ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/></span> </a> </div>
      </div>
      <div class="">
        <div class="">
          <div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a> </div>
        </div>
      </div>
    </div>
  </li>
  <?php
endif;

endforeach;
endif;
?>
</ul>

I want to add pagination in this page, i have tried solutions like following but it does not worked either

parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array(15=>15));
    $pager->setCollection($_categories);

    $this->setChild('pager', $pager);

Can anyone have any idea on how to add pagination in subcategory listing page?

Please guide me as i am new to magento development.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanqianhuan3994 2014-09-01 10:23
    关注

    Inside Mage_Catalog_Block_Navigation I have custom method :

    public function getCurrentChildCategories()
    {
        if (null === $this->_currentChildCategories) {
            $layer = Mage::getSingleton('catalog/layer');
            $category   = $layer->getCurrentCategory();
            $this->_currentChildCategories = Mage::getModel('catalog/category')->getCollection();
    
            $pager = new Mage_Page_Block_Html_Pager();
            $pager->setLimit(100)->setCollection($this->_currentChildCategories);
            $this->setChild('pager', $pager);
    
            /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
            $this->_currentChildCategories->addAttributeToSelect('url_key')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('is_anchor')
                ->addAttributeToSelect('image') 
                ->addAttributeToFilter('is_active', 1)
                ->addIdFilter($category->getChildren())
                ->setOrder('name', 'ASC')
                ->joinUrlRewrite()
                ->load();
    
            $productCollection = Mage::getResourceModel('catalog/product_collection');
            $layer->prepareProductCollection($productCollection);
            $productCollection->addCountToCategories($this->_currentChildCategories);
        }
        return $this->_currentChildCategories;
    }
    

    I think it's not the best but it works.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?