2017-05-14 3 views
0

プライマリナビゲーションメニューにすべてのカテゴリとそのサブカテゴリを表示したいとします。 私はcateoryにカーソルを合わせると、そのサブカテゴリが表示されます。 foreachループでMage :: getModel( 'catalog/category')をロードせずにこの機能を実装したいと思います。あなたは、カテゴリのツリー構造を作成するコードの下PHTMLファイルの使用中のコードを書きたいとforeachループでモデルをロードせずにすべてのカテゴリとサブキャリブレーションを取得

答えて

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

このコードを試してみてください。

とcssとHTMLを使用すると、メインメニューのホバー上にサブメニューを表示するという目標を達成できます。

他のヘルプが必要な場合は、まだ私に教えてください。私はこれをしなかったし、私は誰もがこれを達成するためにどのように思っていたかどうかを確認するために、検索したい考え出して

おかげ

+0

ありがとうございます。 forループを使用すると、サイトが重くなります。モデルをforループに配置したくありません。 – gt06

+0

okこれは役立つかもしれない解決策を確認しましたか? [link](http://iamrookie.com/blog/303/display-all-categories-and-sub-categories-on-left-sidebar-in-magento.html) –

+0

私はすでにこのコードを私のphtmlファイル。私はforループでmage :: getModelをロードしたくありません。 – gt06

0

OK。これにトリックは、このカテゴリのモデルは、それが実際に何をしているのかを見てみましょうロードしない

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との仕事は間違いなくちょうど痛みが可能であることを確認するために、カテゴリオブジェクトのインスタンスを期待するということです。

私はこれをやった唯一の理由は、私の場合のデフォルトのマゼンタのルートカテゴリがカテゴリの実際の機能的ルート(fun eh)ではなかったからです。標準のルートカテゴリを使用している場合は、ヘルパー関数を使用してキャッシュから情報を取得することをお勧めします。

ここからは、Mage_Catalog_Block_Navigationで関数を完成させ、テンプレートにメニューを組み立てるしかありません。そして、そこに行く。モデル - >ロードにアクセスすることなくカテゴリメニューを完成させることができます。

関連する問題