【问题标题】:Magento Product Attributes in top navigation顶部导航中的 Magento 产品属性
【发布时间】: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>

【问题讨论】:

    标签: php magento


    【解决方案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; ?>
    

    【讨论】:

    • 警告:这保证很慢。您应该每天生成一次或其他什么。
    • 你能推荐一个更快的方法吗?我整天都在为此绞尽脑汁@Fuser97381
    • magento 没有什么是快速的,所以你必须缓存所有的东西。在 cron 中生成导航,然后以这种方式卸载它。使 magento 快速运行的唯一方法是尽可能少地使用 magento。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多