【问题标题】:How to get Filterable Attributes from a category in Magento 2如何从 Magento 2 中的类别中获取可过滤属性
【发布时间】:2017-05-04 11:25:28
【问题描述】:

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

  1. 颜色
  2. 尺寸

我正在尝试从“包”类别中获取可过滤属性。

我已经在 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();

但它似乎不适用于 2.x

【问题讨论】:

    标签: magento2


    【解决方案1】:

    我最近遇到了同样的问题。

    我记录了我的调查here

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

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

    $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);
    

    但是,这不是最终结果。为了确保过滤器是真实的,需要检查每个过滤器的项目数。 (该检查实际上是在核心分层导航期间执行的rendering

    $finalFilters = [];
    foreach ($filters as $filter) {
        if ($filter->getItemsCount()) {
            $finalFilters[] = $filter;
        }
    }
    

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

    $name = $filter->getName();
    foreach ($filter->getItems() as $item) {
        $value = $item->getValue();
    }
    

    最后,我想添加替代解决方案,这有点残酷,我想:)

    $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);
    

    那么就可以使用属性id来加载collection了。

     /** @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]);
    

    【讨论】:

    • @Segey:它只返回属性代码。我需要所有可用的带有值的标签
    • 对于过滤器 - 使用 $filter->getName(), $filter->getItems()->getValue()。如果您指的是最后一个解决方案 - 使用 ids 加载 Magento\Catalog\Model \ResourceModel\Product\Attribute\Collection。我会加一行来回答
    • 我通过使用获得重复的过滤器名称:$filter->getName()
    • 嗯,这取决于你的数据。无论如何,这正是 Magento 呈现分层导航的方式:github.com/magento/magento2/blob/develop/app/code/Magento/…
    • 我认为它对我有用。你能再帮我一个忙吗:我需要 $filter 代码和 frontend_input..
    【解决方案2】:

    如果您知道如何构建模块,那么您可以从“module-catalog-graph-ql\Model\Resolver\Layer”中的“FiltersProvider.php”获取帮助。

    use Magento\Catalog\Model\Layer\Category\FilterableAttributeList as CategoryFilterableAttributeList;
    use Magento\Catalog\Model\Layer\FilterListFactory;
    use Magento\Catalog\Model\Layer\Resolver;
    use Magento\Framework\UrlInterface;
    
    public function __construct(
        Resolver $layerResolver,
        FilterListFactory $filterListFactory,
        CategoryFilterableAttributeList $categoryFilterableAttributeList,
        UrlInterface $urlBuilder
    ) {
        $this->_navigation = $navigation;
        $this->layerResolver = $layerResolver;
        $this->filterListFactory = $filterListFactory;
        $this->urlBuilder = $urlBuilder;
        $this->_categoryFilterableAttributeList = $categoryFilterableAttributeList;
    }
    public function getCatMenu($catid)
    {
        $fill_arr = [];
    
        $filterList = $this->filterListFactory->create(['filterableAttributes' => $this->_categoryFilterableAttributeList]);
    
        $layer = clone $this->layerResolver->get();
        $layer->setCurrentCategory($catid);
        $filters =  $filterList->getFilters($layer);
    
        return $fill_arr;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2023-04-04
      • 2017-02-27
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多