magento2 - 如何从 magento 2 中的类别中获取可过滤属性

标签 magento2 magento2.0.2

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

    但是我在 Magento 2.x 中没有得到这个

    请帮我

    最佳答案

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

    我记录了我的调查 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;
        }
    }
    

    然后您可以获得过滤器名称和值。 IE:
    $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 来加载集合。
     /** @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]);
    

    关于magento2 - 如何从 magento 2 中的类别中获取可过滤属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222102/

    相关文章:

    magento2 - 在 Magento2 稳定版中创建新模块?

    php - Magento 2 的产品图片和缩略图显示行为

    php - Magento 停留在 "It' 秒的时间来更改您的密码。”

    php - Magento 2:通过脚本发送订单确认电子邮件

    magento - 在 magento2 中以编程方式保存默认地址

    magento - 在 Magento 2 Rest API 中收到 token 后,如何获取当前签名的用户信息

    nginx - Magento 2通过负载平衡器在多个magento服务器上共享哪些目录?