/**
  * Template method used to configure the attribute codes for the product attributes
  *
  * @return string[]
  */
 public function getCustomAttributesCodes()
 {
     $attributeCodes = array();
     foreach ($this->metadataService->getCustomAttributesMetadata() as $attribute) {
         $attributeCodes[] = $attribute->getAttributeCode();
     }
     return $attributeCodes;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  * Example of request:
  * {
  *     "searchCriteria": {
  *         "filterGroups": [
  *             {
  *                 "filters": [
  *                     {"value": "16.000", "conditionType" : "eq", "field" : "price"}
  *                 ]
  *             }
  *         ]
  *     },
  *     "sort_orders" : {"id": "1"},
  *     "page_size" : "30",
  *     "current_page" : "10"
  * }
  *
  * products?searchCriteria[filterGroups][0][filters][0][field]=price&
  * searchCriteria[filterGroups][0][filters][0][value]=16.000&page_size=30&current_page=1&sort_orders[id]=1
  */
 public function search(SearchCriteria $searchCriteria)
 {
     $this->searchResultsBuilder->setSearchCriteria($searchCriteria);
     /** @var \Magento\Catalog\Model\Resource\Product\Collection $collection */
     $collection = $this->productCollection->create();
     // This is needed to make sure all the attributes are properly loaded
     foreach ($this->metadataService->getProductAttributesMetadata() as $metadata) {
         $collection->addAttributeToSelect($metadata->getAttributeCode());
     }
     $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
     $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     /** @var SortOrder $sortOrder*/
     foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
         $field = $this->translateField($sortOrder->getField());
         $collection->addOrder($field, $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC');
     }
     $collection->setCurPage($searchCriteria->getCurrentPage());
     $collection->setPageSize($searchCriteria->getPageSize());
     $products = array();
     /** @var \Magento\Catalog\Model\Product $productModel */
     foreach ($collection as $productModel) {
         $products[] = $this->converter->createProductDataFromModel($productModel);
     }
     $this->searchResultsBuilder->setItems($products);
     $this->searchResultsBuilder->setTotalCount($collection->getSize());
     return $this->searchResultsBuilder->create();
 }