我在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中得到这个
请帮帮我
答案 0 :(得分:15)
我最近遇到了同样的问题。
我记录了我的调查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来加载集合。
/** @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]);
答案 1 :(得分:-2)
<?php/***********Display Brand Attribute************/?>
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$attribute = $om->get(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::class)->get('manufacturer');
?>
<ul>
<?php
if($attribute){
foreach ($attribute->getOptions() as $option) {
$name = $option->getLabel();
$optId = $option->getValue();
$curnt_cat_url = $category->getUrl();
$brandId = '?manufacturer='.$optId;
$swatchHelper=$om->get("Magento\Swatches\Helper\Media");
$swatchCollection = $om->create('Magento\Swatches\Model\ResourceModel\Swatch\Collection');
$swatchCollection->addFieldtoFilter('option_id',$optId);
$item=$swatchCollection->getFirstItem();
$ThumbImage = $swatchHelper->getSwatchAttributeImage('swatch_thumb', $item->getValue());
//$SwatchImage = $swatchHelper->getSwatchAttributeImage('swatch_image', $item->getValue());
?>
<?php if ($optId) { ?>
<div class="brand_bg">
<li class="brand_img_bg">
<a href="<?php echo $curnt_cat_url.$brandId ?>"><img src="<?php echo $ThumbImage; ?>"></a>
</li>
<!--<a href="<?php //echo $curnt_cat_url.$brandId ?>"><?php //echo $name; ?></a>-->
</div>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>