2012-08-14 141 views
1

我需要从父类中显示4个随机子类。Magento:显示随机子类

我有下面的代码,即与无规产品图像显示从所需要的父类所有子类别(ID = 4): list.phtml \应用\设计\前端\ MyTheme的\默认\模板\目录\类别

<?php 

//迭代在商店 的foreach所有类别($这 - > getChildCategories(4)$ _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; ?>

和navigation.php \程序\代码\本地\法师\目录\座

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; 
    } 

如何限制的子类别4,随机显示它们?谢谢!

回答

1

要限制的集合,你可以添加以下内容:

$collection->setPageSize(4); 

要随机您返回的记录,是possiblities之一:

其中的文件和块
$collection->getSelect() 
    ->order('rand()'); 
+0

我应该添加这些行? – 2012-08-14 19:04:12

+0

'getChildCategories'返回一个集合。所以你想在你调用这个方法的地方添加这个。尽管最好的做法是,你可以通过帮手或其他方式来做到这一点,而不是直接在模板中。 由于集合已由'getChildCategories'中的' - > load()'调用加载,因此您需要先将其重置。因此无论你调用getChildCategories,返回的数据都应该存储在一个变量中(在本例中为$ collection),然后执行$ collection-> clear() - > setPageSize(4) - > getSelect() - > order ( '兰特()'); – 2012-08-14 19:05:30