2016-12-19 59 views
6

我已经在Magento 2.具有过滤属性创建类别“袋”:如何获得可筛选从类别属性在Magento 2

  1. 颜色
  2. 尺寸

我想从“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

请帮我

回答

9

我最近面临同样的问题。

我记录了我的调查here

我没能找到框架API提供特定类别的过滤属性,但是我将分享解决方法。

基本上在Magento 2中的所有过滤属性可以从FilterableAttributeList被retrived:

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

但是,这不是最终结果。为了确保过滤器是实际的,需要检查每个过滤器的项目数量。

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

然后你就可以得到过滤器名和值(即检查核心分层导航rendering期间实际执行)。即:

$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]); 
+0

@Segey:它只返回属性代码。我需要它的所有可用标签值为 –

+0

对于过滤器 - 使用$ filter-> getName(),$ filter-> getItems() - > getValue()。如果您引用最后一个解决方案 - 使用ids加载Magento \ Catalog \型号\ ResourceModel \产品\属性\收藏。我将添加一行来回答 –

+0

好吧让我检查 –