2017-05-14 14 views

回答

0

試試這個代碼

<?php 
require_once("app/Mage.php"); 
Mage::app(); 

function getChildCategories($category, $First = false) { 


     $sub = $First ? $category : $category->getChildren(); 


     foreach ($sub as $child) { 
       $_categories[] = [ "name" => $child->getName(), "id" => $child->getId(), "children" => getChildCategories($child) ]; 
     } 


     return $_categories; 
}; 

function CategoriesTree($category, $First = false) { 


     $sub = $First ? $category : $category->getChildren(); 
     echo "<pre>"; 

     foreach ($sub as $child) { 
      echo $child->getName(); ; 
       CategoriesTree($child); 
     } 

} 

$_categories = Mage::helper('catalog/category')->getStoreCategories(); 


$categories = getChildCategories($_categories, true); 

CategoriesTree($_categories, true); 

?> 
+0

我應該在哪裏寫這段代碼?我想在phtml文件中編寫代碼。 – gt06

0

,你想寫在下面的代碼PHTML文件使用代碼創建類的樹結構。

<ul> 
    <?php 
     $obj = new Mage_Catalog_Block_Navigation(); 
     $storeCategories = $obj->getStoreCategories(); 
     Mage::registry('current_category') ? $currentCategoryId = Mage::registry('current_category')->getId() : $currentCategoryId=''; 
     foreach ($storeCategories as $_category): 
    ?> 
      <li> 
       <strong><?php echo $_category->getName(); ?></strong> 
       <?php $categoryChildren = $_category->getChildren(); ?> 
       <?php if($categoryChildren->count()) : ?> 
        <ul> 

         <?php foreach($categoryChildren as $_categoryChild) : ?> 
          <?php $_categoryChildModel = Mage::getModel('catalog/category')->load($_categoryChild->getId());?> 
          <?php $categoryGrandchildren=$_categoryChild->getChildren(); ?> 
          <li> 
           <?php 
            $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold=''; 
            echo '&emsp;' . '<a href="' . $_categoryChildModel->getUrl() . '"' . $bold . '>' . $_categoryChild->getName() . '(' . $_categoryChildModel->getProductCollection()->count() . ')</a>'; 
           ?> 
          </li> 
          <?php if($categoryGrandchildren->count()) : ?> 
           <?php foreach($categoryGrandchildren as $_categoryGrandchild) : ?> 
            <?php $_categoryGrandchildModel = Mage::getModel('catalog/category')->load($_categoryGrandchild->getId());?> 
            <li> 
             <?php 
              $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold=''; 
              echo '&emsp;&emsp;' . '<a href="' . $_categoryGrandchildModel->getUrl() . '"' . $bold . '>' . $_categoryGrandchild->getName() . '(' . $_categoryGrandchildModel->getProductCount() . ')</a>'; 
             ?> 
            </li> 
           <?php endforeach; ?> 
          <?php endif; ?> 
         <?php endforeach; ?> 
        </ul> 
       <?php endif; ?> 
      </li> 
     <?php endforeach ?> 
</ul> 

和使用CSS和HTML就可以實現你的目標是在主菜單的懸停顯示子菜單。

如果您需要任何其他幫助,請讓我知道。

感謝

+0

謝謝。但是當我們在for循環中使用模型時,網站變得沉重。我不想在for循環中放置模型。 – gt06

+0

好的,你檢查過這個解決方案嗎? [鏈接](http://iamrookie.com/blog/303/display-all-categories-and-sub-categories-on-left-sidebar-in-magento.html) –

+0

我已經把這段代碼放在我的phtml文件。我不想在for循環中加載mage :: getModel。 – gt06

0

行,所以我只是做了這一點,我想我會尋找,看是否有人不知道如何實現這一目標。對此的技巧是

Mage::getResourceSingleton('catalog/category')->getRawAttributeValue($categoryEntityId,array('name','level','url_key','path','is_active'),Mage::app()->getStore()); 

這不加載類別模型讓我們看看它實際上在做什麼。

轉到應用程序/代碼/核心/法師/目錄/型號/資源/摘要

public function getAttributeRawValue($entityId, $attribute, $store) 
{ 
    if (!$entityId || empty($attribute)) { 
     return false; 
    } 
    if (!is_array($attribute)) { 
     $attribute = array($attribute); 
    } 

    $attributesData  = array(); 
    $staticAttributes = array(); 
    $typedAttributes = array(); 
    $staticTable  = null; 
    $adapter   = $this->_getReadAdapter(); 

    foreach ($attribute as $_attribute) { 
     /* @var $attribute Mage_Catalog_Model_Entity_Attribute */ 
     $_attribute = $this->getAttribute($_attribute); 
     if (!$_attribute) { 
      continue; 
     } 
     $attributeCode = $_attribute->getAttributeCode(); 
     $attrTable  = $_attribute->getBackend()->getTable(); 
     $isStatic  = $_attribute->getBackend()->isStatic(); 

     if ($isStatic) { 
      $staticAttributes[] = $attributeCode; 
      $staticTable = $attrTable; 
     } else { 
      /** 
      * That structure needed to avoid farther sql joins for getting attribute's code by id 
      */ 
      $typedAttributes[$attrTable][$_attribute->getId()] = $attributeCode; 
     } 
    } 
    /** 
    * Collecting static attributes 
    */ 
    if ($staticAttributes) { 
     $select = $adapter->select()->from($staticTable, $staticAttributes) 
      ->where($this->getEntityIdField() . ' = :entity_id'); 
     $attributesData = $adapter->fetchRow($select, array('entity_id' => $entityId)); 
    } 

    /** 
    * Collecting typed attributes, performing separate SQL query for each attribute type table 
    */ 
    if ($store instanceof Mage_Core_Model_Store) { 
     $store = $store->getId(); 
    } 

    $store = (int)$store; 
    if ($typedAttributes) { 
     foreach ($typedAttributes as $table => $_attributes) { 
      $select = $adapter->select() 
       ->from(array('default_value' => $table), array('attribute_id')) 
       ->where('default_value.attribute_id IN (?)', array_keys($_attributes)) 
       ->where('default_value.entity_type_id = :entity_type_id') 
       ->where('default_value.entity_id = :entity_id') 
       ->where('default_value.store_id = ?', 0); 
      $bind = array(
       'entity_type_id' => $this->getTypeId(), 
       'entity_id'  => $entityId, 
      ); 

      if ($store != $this->getDefaultStoreId()) { 
       $valueExpr = $adapter->getCheckSql('store_value.value IS NULL', 
        'default_value.value', 'store_value.value'); 
       $joinCondition = array(
        $adapter->quoteInto('store_value.attribute_id IN (?)', array_keys($_attributes)), 
        'store_value.entity_type_id = :entity_type_id', 
        'store_value.entity_id = :entity_id', 
        'store_value.store_id = :store_id', 
       ); 

       $select->joinLeft(
        array('store_value' => $table), 
        implode(' AND ', $joinCondition), 
        array('attr_value' => $valueExpr) 
       ); 

       $bind['store_id'] = $store; 

      } else { 
       $select->columns(array('attr_value' => 'value'), 'default_value'); 
      } 

      $result = $adapter->fetchPairs($select, $bind); 
      foreach ($result as $attrId => $value) { 
       $attrCode = $typedAttributes[$table][$attrId]; 
       $attributesData[$attrCode] = $value; 
      } 
     } 
    } 

    if (sizeof($attributesData) == 1) { 
     $_data = each($attributesData); 
     $attributesData = $_data[1]; 
    } 

    return $attributesData ? $attributesData : false; 
} 

正如你可以看到沒有模型加載發生信息的檢索剛剛專用件。也是資源摘要的一部分,意味着所有的目錄資源模型(我沒有檢查過其他資源模型,但是我不會太驚訝,不能在其他資源模型中找到這個模型)是否可用。

如果您在覆蓋Mage_Catalog_Block_Navigation時使用此選項,則可以在不加載模型的情況下調用所需的任何類別的所有信息。然而,要遍歷樹,你必須做可怕的事情。

您可以使用'路徑'(爆炸/)輕鬆檢索父母,但您需要變髒以檢索兒童類別,以便像這樣獲得兒童。

 $childrenQuery = "SELECT entity_id FROM catalog_category_entity WHERE path REGEXP '^.*\/" . $categoryId . "\/[[:digit:]]?[[:digit:]]?[[:digit:]]?[[:digit:]]?$'"; 
     $resource = Mage::getSingleton('core/resource'); 
     $readCxn = $resource->getConnection('core/read'); 
     $children = $readCxn->fetchAll($childrenQuery); 
     if ($children[0]) { 
      return $children; 
     } else { 
      return; 
     } 

整體難度是所有模型和資源模型函數預計類別對象的實例,使他們只用ENTITY_ID所有的工作是絕對有可能只是一種痛苦。

所以我不會推薦這樣做一般我做這個的唯一原因是因爲我的情況下默認的magento根類別不是實際的類別功能根(fun eh)。如果你使用的是標準的根類別,我會推薦使用幫助函數並從緩存中檢索信息。

從這裏的任何一種方式,你需要做的就是在Mage_Catalog_Block_Navigation中完成你的功能,並將你的菜單組裝到模板中。你去了;完整的類別菜單,而無需訪問模型 - >加載。

相關問題