我想在主導航菜單中顯示所有類別及其子類別。 當我懸停在商品上時,它應該顯示它的子類別。 我想實現這個功能,而無需在foreach循環中加載Mage :: getModel('catalog/category')。在foreach循環中獲取所有類別和子類庫,但不加載模型
0
A
回答
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文件使用代碼創建類的樹結構。
<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 ' ' . '<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 '  ' . '<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
行,所以我只是做了這一點,我想我會尋找,看是否有人不知道如何實現這一目標。對此的技巧是
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中完成你的功能,並將你的菜單組裝到模板中。你去了;完整的類別菜單,而無需訪問模型 - >加載。
相關問題
- 1. 使用api prestashop獲取所有類別和子類別
- 2. 如何獲取所有類別的所有帖子自定義帖子類型?
- 3. 循環的子類別,子類別等
- 4. 獲取所有的子類別中的頂級類別
- 5. 獲取所有模型類型
- 6. WordPress獲取所有類別的帖子
- 7. WordPress的類別,子類別,循環
- 8. 編輯Woocommerce類別/子類別循環
- 9. Laravel獲取類別和子類別
- 10. 有沒有辦法在foreach循環中的所有類型的命名空間?
- 11. 如何獲取Virtuemart中的所有類別和子類別名稱
- 12. 如何從亞馬遜mws獲取所有類別,子類別?
- 13. 使用jQuery獲取和顯示類別和子類別 - 所有的AJAX
- 14. 從WordPress的循環中獲取類別子類別的最新5篇文章
- 15. 通過Wordpress類別與foreach循環
- 16. 我如何獲得所有類別和子類別?
- 17. 從foreach循環中將模型加載到局部視圖中
- 18. 從分類中獲得所有類別的所有帖子
- 19. 獲取所有父類的WooCommerce類別
- 20. 如何獲取C#中所有加載類型的列表?
- 21. 獲取FxCop中所有加載類型的列表
- 22. 獲取類別名稱和類別鏈接裏面的循環在WordPress
- 23. 從woocommerce分類的類別中獲取所有子類別的id(它們的子類別)
- 24. 獲取所有類別或不是正確的類別返回
- 25. 循環通過家庭,類別和子類別
- 26. C#匿名類型foreach循環
- 27. WordPress的 - 將類添加到循環中的子類別
- 28. 獲取所有表的列和類型
- 29. 得到所有的子類別,但沒有在wordpress中指定任何類別
- 30. C#中IEnumerable類的foreach和for循環之間的區別
我應該在哪裏寫這段代碼?我想在phtml文件中編寫代碼。 – gt06