2012-08-14 161 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