【问题标题】:Magento - Get attribute count in categoryMagento - 获取类别中的属性计数
【发布时间】:2014-02-12 21:20:41
【问题描述】:

如何获取特定类别产品的属性值?

我有一个带有根类别的顶部菜单,在鼠标悬停时,显示带有子类别的下拉菜单,我需要显示“品牌”列表。

顶部菜单:

Shoes    |    Cars

“鞋”子菜单:

By Category
    Sport
    Boots
    Work

By Brand
    Adidas
    Ford
    Kia
    Nike
    Le Coq Sportif

“汽车”子菜单:

By Category
    Sedan
    Coupe
    Van

By Brand
    Adidas
    Ford
    Kia
    Nike
    Le Coq Sportif

我想删除 Shoes 下的“Kia”和“Ford”,以及 Cars 下的“Adidas”、“Nike”和“Le Coq Sportif”(某些产品中的品牌除外)

有可能吗?

编辑:

我列出所有品牌使用:

$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setEntityTypeFilter($product->getResource()->getTypeId())
        ->addFieldToFilter('attribute_code', 'brand');

【问题讨论】:

    标签: php magento


    【解决方案1】:

    不确定您需要什么格式,但以下 示例应该说明如何获得您需要的值:

        $attribute = Mage::getModel('eav/entity_attribute')
                        ->loadByCode('catalog_product', 'brand');
    
        $valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                    ->setAttributeFilter($attribute->getData('attribute_id'))
                    ->setStoreFilter(0, false);
    
        $preparedManufacturers = array();            
        foreach($valuesCollection as $value) {
            $preparedManufacturers[$value->getOptionId()] = $value->getValue();
        }   
    
    
        if (count($preparedManufacturers)) {
            echo "<h2>Manufacturers</h2><ul>";
            foreach($preparedManufacturers as $optionId => $value) {
                echo "<li>" . $value . " - (" . $optionId . ")</li>";
            }
            echo "</ul>";
        }
    

    您应该如何获取某个类别的所有制造商:

    $category           = Mage::registry('current_category');
    $layer              = Mage::getSingleton('catalog/layer');
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    $manufacturers = array();
    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'brand') {
            $filterBlockName = 'catalog/layer_filter_attribute';
            $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
            foreach($result->getItems() as $option) {
                $manufacturers[$option->getValue()] = $option->getLabel();
            }
        }
    }
    var_dump($manufacturers);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多