我想在下拉导航中获取产品属性'品牌'。我可以使用以下代码获取属性,但问题是与某些类别无关的品牌属性出现在类别下拉列表中。所以我需要的是品牌属性只有当该类别中的产品具有品牌属性时才出现在类别中。例如。品牌'精工'将出现在手表类别下降但不是项链类别下降。任何帮助将不胜感激!
<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>
答案 0 :(得分:1)
我已经找到了感兴趣的人..我没有验证产品集合的属性选项,下面是更正后的代码。
<?php foreach($validAttributes as $validAttribute) : ?>
<?php
if ($validAttribute->getAttributeCode() == 'brand') {
$options = $validAttribute->getSource()->getAllOptions();
foreach ($options as $option) {
//Validate the product collection against each attribute option
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addCategoryFilter($_categoryModel);
$collection->addAttributeToFilter('brand',$option);
//if the count is greater than 0 after the collection has been filtered by category and product attribute option, echo the attribute.
if (count($collection) > 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; ?>