【发布时间】:2015-08-21 14:57:29
【问题描述】:
我正在尝试在下拉导航中获取产品属性“品牌”。我可以使用以下代码显示属性,但问题是与某些类别不相关的品牌属性出现在类别下拉列表中。所以我需要的是,当该类别中存在具有品牌属性的产品时,品牌属性仅出现在该类别中。例如。品牌“精工”将出现在手表类别下拉列表中,但不会出现在项链类别下拉列表中。任何帮助将不胜感激!
<ul class="nav container">
<?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):
?>
<?php // Top level Nav - categories ?>
<li>
<?php $_categoryModel = Mage::getModel('catalog/category')->load($_category->getId());?>
<?php $catUrl = $_categoryModel->getUrl(); ?>
<?php echo $_category->getName(); ?>
<div>
<?php $categoryChildren = $_category->getChildren(); ?>
<?php if($categoryChildren->count()) : ?>
<?php // Drop downs to subcategories - works fine. ?>
<div class="one-third column mega-menu">
<p class="menu-header">Category</p>
<ul class="twelve columns">
<?php foreach($categoryChildren as $_categoryChild) : ?>
<?php $_categoryChildModel = Mage::getModel('catalog/category')->load($_categoryChild->getId());?>
<li>
<?php
$currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
echo '<a href="' . $_categoryChildModel->getUrl() . '"' . $bold . '>' . $_categoryChild->getName() . '</a>';
?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php // Drop downs to brand attribute - currently displays all brands not just relevant ones ?>
<div class="one-third column mega-menu">
<p class="menu-header">Brand</p>
<ul class="twelve columns">
<?php
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory(Mage::getModel('catalog/category')->load($_category->getId()));
$nodeId = $_category->getId();
$id = str_replace('category-node-', '', $nodeId);
$validAttributes = array();
foreach ($layer->getFilterableAttributes() as $attribute) {
//I think this is where the validation needs to go but I dont know how to get only products within the category with brand attributes
$validAttributes[] = $attribute;
}
?>
<?php foreach($validAttributes as $validAttribute) : ?>
<?php
if ($validAttribute->getAttributeCode() == 'brand') {
$options = $validAttribute->getSource()->getAllOptions();
foreach ($options as $option) {
$productCount = Mage::getModel('catalog/product')->getCollection()->groupByAttribute('country_of_manufacture')
->addExpressionAttributeToSelect("cnt_product",'COUNT({{entity_id}})', 'entity_id')
->load();
if (count($option) > 0) {
$optionNodeId = 'attribute-'.$id.'-'.$validAttribute->getId().'-'.$option['value'];
$data = array(
'name' => $option['label'],
'id' => $optionNodeId,
'url' => $catUrl.'?'.$validAttribute->getAttributeCode().'='.$option['value']
);
echo '<li><a href="'.$data['url'].'">'.$data['name'].'</a></li>';
}
}
}
?>
<?php endforeach; ?>
</ul>
</div>
</div>
</li>
<?php endforeach ?>
<li><a href="<?php echo Mage::getBaseUrl(); ?>services">Services</a></li>
<li><a href="<?php echo Mage::getBaseUrl(); ?>contact">Contact</a></li>
</ul>
【问题讨论】: