Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. public function __construct(
  2. MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
  3. MagentoCatalogApiDataProductSearchResultsInterfaceFactory $searchResultsFactory,
  4. MagentoFrameworkApiExtensionAttributeJoinProcessorInterface $extensionAttributesJoinProcessor) {
  5.  
  6. $this->productCollection = $productCollectionFactory;
  7. $this->searchResultsFactory = $searchResultsFactory;
  8. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  9. }
  10.  
  11. /**
  12. * Return the sum of the two numbers.
  13. *
  14. * @api
  15. * @param string $query.
  16. * @return MagentoFrameworkApiSearchResults
  17. */
  18. public function products($query) {
  19.  
  20. $collection = $this->productCollection->create();
  21. $this->extensionAttributesJoinProcessor->process($collection);
  22.  
  23. $collection->addAttributeToSelect('*');
  24. $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
  25. $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
  26.  
  27. $collection->setCurPage(1);
  28. $collection->setPageSize(100);
  29. $collection->load();
  30.  
  31. $searchResult = $this->searchResultsFactory->create();
  32. $searchResult->setItems($collection->getItems());
  33. $searchResult->setTotalCount($collection->getSize());
  34.  
  35. return $searchResult;
  36. }
  37.  
  38. public function getList(MagentoFrameworkApiSearchCriteriaInterface $searchCriteria)
  39. {
  40. /** @var MagentoCatalogModelResourceModelProductCollection $collection */
  41. $collection = $this->collectionFactory->create();
  42. $this->extensionAttributesJoinProcessor->process($collection);
  43.  
  44. foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() as $metadata) {
  45. $collection->addAttributeToSelect($metadata->getAttributeCode());
  46. }
  47. $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
  48. $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
  49.  
  50. //Add filters from root filter group to the collection
  51. foreach ($searchCriteria->getFilterGroups() as $group) {
  52. $this->addFilterGroupToCollection($group, $collection);
  53. }
  54. /** @var SortOrder $sortOrder */
  55. foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
  56. $field = $sortOrder->getField();
  57. $collection->addOrder(
  58. $field,
  59. ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
  60. );
  61. }
  62. $collection->setCurPage($searchCriteria->getCurrentPage());
  63. $collection->setPageSize($searchCriteria->getPageSize());
  64. $collection->load();
  65.  
  66. $searchResult = $this->searchResultsFactory->create();
  67. $searchResult->setSearchCriteria($searchCriteria);
  68. $searchResult->setItems($collection->getItems());
  69. $searchResult->setTotalCount($collection->getSize());
  70. return $searchResult;
  71. }
  72.  
  73. <preference for="tommyjsProductsApiProductsInterface"
  74. type="tommyjsProductsModelProductsEndpoint" />
  75.  
  76. <type name="tommyjsProductsModelProductsEndpoint">
  77. <arguments>
  78. <argument name="productCollectionFactory" xsi:type="object">MagentoCatalogModelResourceModelProductCollectionFactory</argument>
  79. </arguments>
  80. </type>
  81.  
  82. /**
  83. * Return the sum of the two numbers.
  84. *
  85. * @api
  86. * @param string $query.
  87. * @return MagentoFrameworkApiSearchResults
  88. */
  89. public function products($query) {
  90.  
  91. /**
  92. * Return the sum of the two numbers.
  93. *
  94. * @api
  95. * @param string $query.
  96. * @return MagentoCatalogApiDataProductSearchResultsInterface
  97. */
  98. public function products($query) {
  99.  
  100. #vendor/magento/module-catalog/etc/webapi.xml
  101.  
  102. <route url="/V1/products1/:query" method="GET">
  103. <service class="MagentoCatalogApiProductRepositoryInterface" method="products1"/>
  104. <resources>
  105. <resource ref="anonymous" />
  106. </resources>
  107. </route>
  108.  
  109.  
  110. #vendor/magento/module-catalog/Api/ProductRepositoryInterface.php
  111.  
  112. /**
  113. * Return the sum of the two numbers.
  114. *
  115. * @api
  116. * @param string $query|null.
  117. * @return MagentoFrameworkApiSearchResults
  118. */
  119. public function products1($query);
  120.  
  121.  
  122. # vendor/magento/module-catalog/Model/ProductRepository.php
  123.  
  124. public function products1($query) {
  125. #MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
  126. $collection = $this->productCollection->create();
  127. $this->extensionAttributesJoinProcessor->process($collection);
  128.  
  129. $collection->addAttributeToSelect('*');
  130. $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
  131. $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
  132.  
  133. $collection->setCurPage(1);
  134. $collection->setPageSize(100);
  135. $collection->load();
  136.  
  137. $searchResult = $this->searchResultsFactory->create();
  138. $searchResult->setItems($collection->getItems());
  139. $searchResult->setTotalCount($collection->getSize());
  140.  
  141. return $searchResult;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement