douwen4401 2012-08-14 17:18
浏览 13
已采纳

Magento:显示随机子类别

I need to display 4 random subcategories from a parent category.

I have the following code, that displays ALL subcategories from the needed parent category (ID=4) with random product images: list.phtml \app\design\frontend\MYTHEME\default\template\catalog\category

<?php

// Iterate all categories in store foreach ($this->getChildCategories(4) as $_childCategory):

   // If category is Active
   if($_childCategory->getIsActive()):

       // Load the actual category object for this category
       $cur_category = Mage::getModel('catalog/category')->load($_childCategory->getId());


       // Load a random product from this category
       $products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->addAttributeToSelect('small_image');
       $products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);

       $products->load();

       // This a bit of a fudge - there's only one element in the collection
       $_product = null;
       foreach ( $products as $_product ) {}        
       ?>

       <div style="float: left; padding-right: 30px; text-align: center;">
           <div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_childCategory)?>">

       <?php
       if(isset($_product)):
       ?>
       <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" alt="<?php echo $_childCategory->getName()?>" title="<?php echo $_childCategory->getName()?>" />
       <?php
       endif;
       ?>
           </div>
           <?php echo $_childCategory->getName()?></a></p>
       </div>
       <?php
   endif;

endforeach; ?>

and in navigation.php \app\code\local\Mage\Catalog\Block

public function getChildCategories($categoryId) //inserted for random subcategories on category page
    {
          $category = Mage::getModel('catalog/category');
          if($category->checkId($categoryId) === false) {
              return false;
          }
        $layer = Mage::getSingleton('catalog/layer');
        $category->load($categoryId);
        $layer->setCurrentCategory($category);
        /* @var $category Mage_Catalog_Model_Category */
        $collection = Mage::getModel('catalog/category')->getCollection();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        $collection->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
            ->joinUrlRewrite()
            ->load();

        //$productCollection = Mage::getResourceModel('catalog/product_collection');
        //$layer->prepareProductCollection($productCollection);
        //$productCollection->addCountToCategories($collection);
        return $collection;
    }

How can I limit the subcategories to 4 and display them randomly? Thanks!

  • 写回答

1条回答 默认 最新

  • dongziya9863 2012-08-14 18:22
    关注

    To limit a collection you can add the following:

    $collection->setPageSize(4);
    

    To randomize your returned records, is one of the possiblities:

    $collection->getSelect()
        ->order('rand()');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?