web-dev-qa-db-ja.com

Magento2のカテゴリからフィルタリング可能な属性を取得する方法

Magento 2でカテゴリ「バッグ」を作成しました。フィルタ属性は次のとおりです。

  1. サイズ

カテゴリ「バッグ」からフィルタリング可能な属性を取得しようとしています。

解決策は見つかりませんでした。

私はすでにmagento1.9でこれを同じようにしています:

Mage::app()->setCurrentStore($store);
$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($categoryid);  
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();

しかし、Magento2.xではこれを取得できませんでした

私を助けてください

10
Vishal Kamal

私は最近同じ問題に直面しました。

私は自分の調査を文書化しました ここ

特定のカテゴリにフィルタリング可能な属性を提供するフレームワークAPIを見つけることができませんでしたが、回避策を共有します。

基本的に、Magento 2のすべてのフィルター可能な属性はFilterableAttributeListから取得できます。

$filterableAttributes = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
$attributes = $filterableAttributes->getList();

ObjectManager :: getInstance()の代わりにDIを使用してください。もっとコンパクトな例にするためだけに使用しました:)

階層化されたナビゲーションに関連するフィルターの取得は、もう少し注意が必要です。

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

次に、フィルターの名前と値を取得できます。すなわち:

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