我在 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/