Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function getChildren($productId)
 {
     $product = $this->productRepository->get($productId);
     if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
         return [];
     }
     $childrenList = [];
     /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
     $productTypeInstance = $product->getTypeInstance();
     $productTypeInstance->setStoreFilter($product->getStoreId(), $product);
     foreach ($productTypeInstance->getUsedProducts($product) as $child) {
         $childrenList[] = $this->productConverter->createProductDataFromModel($child);
     }
     return $childrenList;
 }
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();
 }
Exemple #3
0
 public function testGetChildren()
 {
     $productId = 'sadasd';
     $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock();
     $productTypeInstance = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable')->disableOriginalConstructor()->getMock();
     $product->expects($this->any())->method('getTypeId')->will($this->returnValue(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE));
     $product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($productTypeInstance));
     $productTypeInstance->expects($this->once())->method('setStoreFilter')->with(null, $product);
     $childProduct = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock();
     $productTypeInstance->expects($this->any())->method('getUsedProducts')->with($product)->will($this->returnValue([$childProduct]));
     $this->productRepository->expects($this->any())->method('get')->with($productId)->will($this->returnValue($product));
     $productDto = $this->getMockBuilder('Magento\\Catalog\\Service\\V1\\Data\\Product')->disableOriginalConstructor()->getMock();
     $this->productConverter->expects($this->any())->method('createProductDataFromModel')->with($childProduct)->will($this->returnValue($productDto));
     $products = $this->object->getChildren($productId);
     $this->assertCount(1, $products);
     $this->assertEquals($productDto, $products[0]);
 }