I want to create custom API module that will return the same thing as: /V1/products
REST API endpoint.
In order to do that I created the following custom endpoint:
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory $searchResultsFactory,
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor) {
$this->productCollection = $productCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
}
/**
* Return the sum of the two numbers.
*
* @api
* @param string $query.
* @return \Magento\Framework\Api\SearchResults
*/
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;
}
I created it with analogy to core /V1/products/getList
:
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $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;
}
Nevertheless, my solution return only custom_attributes (no name, no price etc.). It does not return them even if I do so:
$collection->addAttributeToSelect('name');
Do you know what can be the reason? I will be really grateful for any help.
Additionally I upload my di.xml
file:
<preference for="tommyjs\Products\Api\ProductsInterface"
type="tommyjs\Products\Model\ProductsEndpoint" />
<type name="tommyjs\Products\Model\ProductsEndpoint">
<arguments>
<argument name="productCollectionFactory" xsi:type="object">\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory</argument>
</arguments>
</type>
SOLUTION
I resolved the problem by replacing:
/**
* Return the sum of the two numbers.
*
* @api
* @param string $query.
* @return \Magento\Framework\Api\SearchResults
*/
public function products($query) {
with
/**
* Return the sum of the two numbers.
*
* @api
* @param string $query.
* @return \Magento\Catalog\Api\Data\ProductSearchResultsInterface
*/
public function products($query) {