我试图在下拉导航中获得产品属性'brand'。我可以得到的属性出现以下代码,但问题是品牌属性是不相关的某些类别出现在类别下拉。所以我需要的是品牌属性只出现在一个类别中,当有产品在该类别与品牌属性。如。品牌"精工"将出现在手表类别下拉,而不是项链类别下拉。任何帮助将非常感激!
<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>
我为感兴趣的人弄明白了…我没有针对产品集合验证属性选项,下面是更正后的代码。
<?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; ?>