我正在创建api以获取magento 2中特定类别的可过滤属性。
我们在分类页面中看到分层导航。我想在api中获得完全相同的数据。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
$appState = $objectManager->get(\Magento\Framework\App\State::class);
$layerResolver = $objectManager->get(\Magento\Catalog\Model\Layer\Resolver::class);
$filterList = $objectManager->create(
\Magento\Catalog\Model\Layer\FilterList::class,
[
'filterableAttributes' => $filterableAttributes
]
);
$category_id = 41;
// $appState->setAreaCode('frontend');
$layer = $layerResolver->get();
$layer->setCurrentCategory($category_id);
$filters = $filterList->getFilters($layer);
$finalFilters = [];
$data = array();
$i = 0;
foreach ($filters as $filter) {
if ($filter->getItemsCount()) {
$name = $filter->getName();
foreach ($filter->getItems() as $item) {
if($item->getFilter()->getAttributeModel()->getAttributeCode() == 'price'){
$finalFilters[$name][$i]['name'] = $this->Helper->removeHTML($item->getLabel()->getArguments());
$finalFilters[$name][$i]['value'] = $item->getValue();
$finalFilters[$name][$i]['code'] = $item->getFilter()->getAttributeModel()->getAttributeCode();
}
else{
$finalFilters[$name][$i]['name'] = $item->getLabel();
$finalFilters[$name][$i]['value'] = $item->getValue();
$finalFilters[$name][$i]['code'] = $item->getFilter()->getAttributeModel()->getAttributeCode();
}
$i++;
}
}
}
但是得到以下错误。
异常#0(Magento \ Framework \ Exception \ LocalizedException):未定义属性模型。
答案 0 :(得分:0)
因为在过滤器中有类别所以它没有属性模型所以使用下面的函数来实现那个
public function filters($categoryId)
{
$categoryLayer = $this->layerResolver->get()->setCurrentCategory($categoryId);
$category = $this->getCategory();
$filterList = new FilterList($this->objectManager, $this->fill);
$filterAttributes = $filterList->getFilters($categoryLayer);
$filterArray = [];
$i = 0;
foreach ($filterAttributes as $filter) {
$attributeLabel = (string) $filter->getName();
$attributeCode = (string) $filter->getRequestVar();
$items = $filter->getItems();
$filterValues = [];
$j = 0;
foreach ($items as $item) {
if ($attributeCode == 'cat') {
$filterValues[$j]['display'] = strip_tags($item->getLabel());
$filterValues[$j]['value'] = $item->getValue();
} elseif ($category->getIsAnchor()) {
if ($filter->getAttributeModel()
&& $filter->getAttributeModel()->getFrontendInput() == 'price') {
$filterValues[$j]['min_price'] = $filter->getLayer()->getProductCollection()->getMinPrice();
$filterValues[$j]['max_price'] = $filter->getLayer()->getProductCollection()->getMaxPrice();
break;
}
$filterValues[$j]['display'] = strip_tags($item->getLabel());
$filterValues[$j]['value'] = $item->getValue();
// Get Swatches.
$swatchesValues = $this->getSwatches($filter, $item, $j);
if (!empty($swatchesValues)) {
$filterValues[$j]['swatch_value'] = $swatchesValues['swatch_value'];
$filterValues[$j]['swatch_type'] = $swatchesValues['swatch_type'];
}
}
$j++;
}
if (!empty($filterValues)) {
$filterArray['available_filter'][$attributeCode]['label'] = $attributeLabel;
$filterArray['available_filter'][$attributeCode]['values'] = $filterValues;
}
$i++;
}
return [$filterArray];
}//end filters()
答案 1 :(得分:0)
<?php
namespace Example\ExtendedApi\Model;
use Example\ExtendedApi\Api\FiltersInterface;
class FiltersManagement implements FiltersInterface
{
protected $_request;
protected $_filterableAttributeList;
protected $_layerResolver;
protected $_filterList;
protected $_storeManagerInterface;
protected $_response;
protected $_redirFactory;
public function __construct(
\Magento\CatalogGraphQl\Model\Resolver\Layer\FilterableAttributesListFactory $filterableAttributeList,
\Magento\Catalog\Model\Layer\FilterListFactory $filterList,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Framework\Webapi\Rest\Request $request
)
{
$this->_filterList = $filterList;
$this->_filterableAttributeList = $filterableAttributeList;
$this->_layerResolver = $layerResolver;
$this->_request = $request;
$this->_storeManagerInterface = $storeManagerInterface;
}
/**
* GET review by its ID
*
* @api
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
*/
public function getLayeredFilters()
{
$categoryId = $this->_request->getParam('categoryId');
$layer = $this->_layerResolver->get();
$layerType = "search";
if ($categoryId){
$layer->setCurrentCategory($categoryId);
$layerType = "category";
}
$filterArray['store_id'] = $this->_storeManagerInterface->getStore()->getId();
$filterableAttributesList = $this->_filterableAttributeList->create($layerType);
$filterList = $this->_filterList->create(['filterableAttributes' => $filterableAttributesList]);
$filters = $filterList->getFilters($layer);
$i = 0;
foreach($filters as $filter)
{
// Don't show options with no items
if (! $filter->getItemsCount()) {continue;}
$availablefilter = (string)$filter->getName();
$items = $filter->getItems();
$filterValues = array();
$j = 0;
foreach($items as $item)
{
$filterValues[$j]['display'] = strip_tags($item->getLabel());
$filterValues[$j]['value'] = $item->getValue();
$filterValues[$j]['count'] = $item->getCount(); //Gives no. of products in each filter options
$filterValues[$j]['url'] = $item->getUrl(); //Gives filter url.
$j++;
}
if(!empty($filterValues) && count($filterValues)>1)
{
$filterArray['availablefilter'][$availablefilter] = $filterValues;
}
$i++;
}
if (!isset($filterArray["availablefilter"])) {
$filterArray['availablefilter'] = "No filters to show.";
}
header("Content-Type: application/json; charset=utf-8");
$this->response = json_encode($filterArray);
print_r($this->response,false);
die();
}
}