Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function __construct(
- MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
- MagentoCatalogApiDataProductSearchResultsInterfaceFactory $searchResultsFactory,
- MagentoFrameworkApiExtensionAttributeJoinProcessorInterface $extensionAttributesJoinProcessor) {
- $this->productCollection = $productCollectionFactory;
- $this->searchResultsFactory = $searchResultsFactory;
- $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
- }
- /**
- * Return the sum of the two numbers.
- *
- * @api
- * @param string $query.
- * @return MagentoFrameworkApiSearchResults
- */
- public function products($query) {
- $collection = $this->productCollection->create();
- $this->extensionAttributesJoinProcessor->process($collection);
- $collection->addAttributeToSelect('*');
- $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
- $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
- $collection->setCurPage(1);
- $collection->setPageSize(100);
- $collection->load();
- $searchResult = $this->searchResultsFactory->create();
- $searchResult->setItems($collection->getItems());
- $searchResult->setTotalCount($collection->getSize());
- return $searchResult;
- }
- public function getList(MagentoFrameworkApiSearchCriteriaInterface $searchCriteria)
- {
- /** @var MagentoCatalogModelResourceModelProductCollection $collection */
- $collection = $this->collectionFactory->create();
- $this->extensionAttributesJoinProcessor->process($collection);
- foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() 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 = $sortOrder->getField();
- $collection->addOrder(
- $field,
- ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
- );
- }
- $collection->setCurPage($searchCriteria->getCurrentPage());
- $collection->setPageSize($searchCriteria->getPageSize());
- $collection->load();
- $searchResult = $this->searchResultsFactory->create();
- $searchResult->setSearchCriteria($searchCriteria);
- $searchResult->setItems($collection->getItems());
- $searchResult->setTotalCount($collection->getSize());
- return $searchResult;
- }
- <preference for="tommyjsProductsApiProductsInterface"
- type="tommyjsProductsModelProductsEndpoint" />
- <type name="tommyjsProductsModelProductsEndpoint">
- <arguments>
- <argument name="productCollectionFactory" xsi:type="object">MagentoCatalogModelResourceModelProductCollectionFactory</argument>
- </arguments>
- </type>
- /**
- * Return the sum of the two numbers.
- *
- * @api
- * @param string $query.
- * @return MagentoFrameworkApiSearchResults
- */
- public function products($query) {
- /**
- * Return the sum of the two numbers.
- *
- * @api
- * @param string $query.
- * @return MagentoCatalogApiDataProductSearchResultsInterface
- */
- public function products($query) {
- #vendor/magento/module-catalog/etc/webapi.xml
- <route url="/V1/products1/:query" method="GET">
- <service class="MagentoCatalogApiProductRepositoryInterface" method="products1"/>
- <resources>
- <resource ref="anonymous" />
- </resources>
- </route>
- #vendor/magento/module-catalog/Api/ProductRepositoryInterface.php
- /**
- * Return the sum of the two numbers.
- *
- * @api
- * @param string $query|null.
- * @return MagentoFrameworkApiSearchResults
- */
- public function products1($query);
- # vendor/magento/module-catalog/Model/ProductRepository.php
- public function products1($query) {
- #MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
- $collection = $this->productCollection->create();
- $this->extensionAttributesJoinProcessor->process($collection);
- $collection->addAttributeToSelect('*');
- $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
- $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
- $collection->setCurPage(1);
- $collection->setPageSize(100);
- $collection->load();
- $searchResult = $this->searchResultsFactory->create();
- $searchResult->setItems($collection->getItems());
- $searchResult->setTotalCount($collection->getSize());
- return $searchResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement