Magento2 如何从magento 2中的类别中获取可筛选属性

Magento2 如何从magento 2中的类别中获取可筛选属性,magento2,magento2.0.2,Magento2,Magento2.0.2,我在magento 2中创建了类别“Bag”。具有过滤器属性: 颜色 大小 我正在尝试从类别“Bag”中获取可过滤属性 我没有找到任何解决办法 我已经在magento 1.9中做过同样的操作: Mage::app()->setCurrentStore($store); $layer = Mage::getModel("catalog/layer"); $category = Mage::getModel("catalog/category")->load($categoryid);

我在magento 2中创建了类别“Bag”。具有过滤器属性:

  • 颜色
  • 大小
  • 我正在尝试从类别“Bag”中获取可过滤属性

    我没有找到任何解决办法

    我已经在magento 1.9中做过同样的操作:

    Mage::app()->setCurrentStore($store);
    $layer = Mage::getModel("catalog/layer");
    $category = Mage::getModel("catalog/category")->load($categoryid);  
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    
    但是我在Magento 2.x中没有得到这个


    请帮帮我,我最近也遇到了同样的问题

    我记录了我的调查

    我无法找到框架api为特定类别提供可过滤属性,但我将分享解决方法

    基本上,Magento 2中的所有可过滤属性都可以从可过滤属性列表中检索:

    $filterableAttributes = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
    $attributes = $filterableAttributes->getList();
    
    请使用DI而不是ObjectManager::getInstance()。我使用它只是为了得到更紧凑的示例:)

    检索分层导航中涉及的过滤器有点棘手

    $filterableAttributes = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
    
    $appState = ObjectManager::getInstance()->get(\Magento\Framework\App\State::class);
    $layerResolver = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class);
    $filterList = ObjectManager::getInstance()->create(
    \Magento\Catalog\Model\Layer\FilterList::class,
        [
            'filterableAttributes' => $filterableAttributes
        ]
    );
    
    $category = 1234;
    
    $appState->setAreaCode('frontend');
    $layer = $layerResolver->get();
    $layer->setCurrentCategory($category);
    $filters = $filterList->getFilters($layer);
    
    然而,这并不是最终结果。为了确保过滤器是实际的,需要检查每个过滤器的项目数。(该检查实际上是在核心分层导航期间执行的)

    然后您可以获得过滤器名称和值。即:

    $name = $filter->getName();
    foreach ($filter->getItems() as $item) {
        $value = $item->getValue();
    }
    
    最后,我想添加另一种解决方案,这有点残酷,我想:)

    然后可以使用属性ID加载集合

     /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
    $collection = $this->collectionFactory->create();
    $collection->setItemObjectClass('Magento\Catalog\Model\ResourceModel\Eav\Attribute')
            ->addStoreLabel($this->storeManager->getStore()->getId());
    $collection->addFieldToFilter('attribute_id', ['in' => $attributeIds]);
    

    @它只返回属性代码。我需要它的所有可用标签与过滤器的值-使用$filter->getName(),$filter->getItems()->getValue()。如果您指的是上一个解决方案-使用ID加载Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection。我将在回答中添加一行,好的,让我检查一下,我使用:$filter->getName()获得了重复的筛选器名称,这取决于您的数据。无论如何,这正是Magento渲染分层导航的方式:
    $categoryId = 1234;
    
    $resource = ObjectManager::getInstance()->get(\Magento\Framework\App\ResourceConnection::class);
    $connection = $resource->getConnection();
    
    $select = $connection->select()->from(['ea' => $connection->getTableName('eav_attribute')], 'ea.attribute_id')
    ->join(['eea' => $connection->getTableName('eav_entity_attribute')], 'ea.attribute_id = eea.attribute_id')
    ->join(['cea' => $connection->getTableName('catalog_eav_attribute')], 'ea.attribute_id = cea.attribute_id')
    ->join(['cpe' => $connection->getTableName('catalog_product_entity')], 'eea.attribute_set_id = cpe.attribute_set_id')
    ->join(['ccp' => $connection->getTableName('catalog_category_product')], 'cpe.entity_id = ccp.product_id')
    ->where('cea.is_filterable = ?', 1)
    ->where('ccp.category_id = ?', $categoryId)
    ->group('ea.attribute_id');
    
    $attributeIds = $connection->fetchCol($select);
    
     /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
    $collection = $this->collectionFactory->create();
    $collection->setItemObjectClass('Magento\Catalog\Model\ResourceModel\Eav\Attribute')
            ->addStoreLabel($this->storeManager->getStore()->getId());
    $collection->addFieldToFilter('attribute_id', ['in' => $attributeIds]);