2012-03-18 111 views
0

我有一些調用主類別的子類別的代碼,我需要能夠更改網站前端的子類別的排序順序。更改magento中子類別的排序順序

我已經嘗試添加屬性來排序標記,但這不是做任何事情。任何人都可以幫助我指出正確的方向來實現這一目標。許多感謝:

->addAttributeToSort(’position’, ‘asc’) 

這是沒有任何影響的順序。我使用的代碼如下:

  <?php 
     //get the current category 
     $_cat = new Mage_Catalog_Block_Navigation(); 
     $currentCat = $_cat->getCurrentCategory(); 

     //get the children of the current category 
     $subCats = Mage::getModel('catalog/category')->load($currentCat->getId())->getChildren(); 
     //get sub category ids 
     $subCatIds = explode(',',$subCats); 
     ?> 
     <?php if (count($subCatIds) > 1): ?> 
     <?php foreach($subCatIds as $subCatId): ?> 
     <?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?> 
     <?php if($subCat->getIsActive()): ?> 

回答

0

你可以試試這個:

您可以編寫代碼後$ subCats做foreach $ subCatIds

$collection = Mage::getModel('catalog/category')->getCollection() 
       ->addAttributeToFilter('entity_id', array('in' => $subCats)) 
       ->addAttributeToSelect('entity_id'); 
if($collection) 
{ 
    $subCatIds = $collection->setOrder('position', 'asc'); 
} 

後,你可以用走id。希望這會有所幫助...

3

@Jason Millward 請不要對每個對象調用load()。它會影響網站在不久的將來的表現;)
我爲你創建了一個例子。

$currentCategory = Mage::getModel('catalog/category')->load(3); 
    $collection = $currentCategory->getCollection(); 
    $collection->addAttributeToSelect('url_key') 
     ->addAttributeToSelect('name') 
     ->addAttributeToSelect('all_children') 
     ->addAttributeToSelect('is_anchor') 
     ->addAttributeToFilter('is_active', 1) 
     ->addIdFilter($currentCategory->getChildren()) 
     ->setOrder('position', Varien_Db_Select::SQL_ASC) 
     ->load(); 

類別實體已經擁有從admin管理的位置屬性。
只需使用它來訂購類別。

+0

謝謝你,但我在哪裏添加這個代碼來調用它?很多感謝 – 2012-03-19 06:59:04

+0

好,取決於你想要的地方:) – Sergey 2012-03-19 08:20:14

+0

它可以被添加到我已經張貼在上面的代碼或我需要看看重寫它,以使這種合適? – 2012-03-19 11:14:17

0

如果您想要按位置排序加載特定類別的子類別。請試試這個:

/** @var Mage_Catalog_Model_Resource_Category_Flat_Collection $storeCategories */ 
$storeCategories = Mage::getModel('catalog/category')->getCategories(
    $currentCategory->getId(), 0, false, true, false 
); 
$storeCategories->unshiftOrder('position', Varien_Db_Select::SQL_ASC); 

通知,我們必須使用,而不是unshiftOrdersetOrder。否則,在加載集合之前,它將首先按名稱排序。
我參考了頂層菜單來看看它是如何工作的。希望這會幫助別人!