I want to get new product using REST API.
I am using below code:-
app/code/Myvendor/Mymodule/Api/NewproductInterface.php
<?php
namespace Myvendor\Mymodule\Api;
//use Myvendor\Mymodule\Api\Data\PointInterface;
interface NewproductInterface
{
/**
* GET new product list
*
* @api
* @param string $id
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function newproduct();
}
app/code/Myvendor/Mymodule/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<!-- Get new peoduct list -->
<route url="/V1/custom/catalog/newproduct" method="POST">
<service class="Myvendor\Mymodule\Api\NewproductInterface" method="newproduct"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
app/code/Myvendor/Mymodule/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Myvendor\Mymodule\Api\NewproductInterface" type="Myvendor\Mymodule\Model\Newproduct" />
</config>
app/code/Myvendor/Mymodule/Model/Newproduct.php
use Magento\Catalog\Api\ProductRepositoryInterface;
use Myvendor\Mymodule\Api\NewproductInterface;
class Newproduct implements NewproductInterface
{
const DEFAULT_PRODUCTS_COUNT = 10;
//protected $searchCriteriaBuilder;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
protected $_localeDate;
protected $_catalogConfig;
protected $_productsCount;
public function __construct(
ProductRepositoryInterface $productRepository,
//SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Catalog\Block\Product\Context $context
)
{
$this->productRepository = $productRepository;
//$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_productCollectionFactory = $productCollectionFactory;
$this->_catalogProductVisibility = $catalogProductVisibility;
$this->_localeDate = $localeDate;
$this->_catalogConfig = $context->getCatalogConfig();
}
protected function _addProductAttributesAndPrices(
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
) {
return $collection
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
->addUrlRewrite();
}
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
/**
* {@inheritdoc}
*/
public function newproduct()
{
$todayStartOfDayDate = $this->_localeDate->date()->setTime(0, 0, 0)->format('Y-m-d H:i:s');
$todayEndOfDayDate = $this->_localeDate->date()->setTime(23, 59, 59)->format('Y-m-d H:i:s');
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->_productCollectionFactory->create();
$collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices(
$collection
)->addStoreFilter()->addAttributeToFilter(
'news_from_date',
[
'or' => [
0 => ['date' => true, 'to' => $todayEndOfDayDate],
1 => ['is' => new \Zend_Db_Expr('null')],
]
],
'left'
)->addAttributeToFilter(
'news_to_date',
[
'or' => [
0 => ['date' => true, 'from' => $todayStartOfDayDate],
1 => ['is' => new \Zend_Db_Expr('null')],
]
],
'left'
)->addAttributeToFilter(
[
['attribute' => 'news_from_date', 'is' => new \Zend_Db_Expr('not null')],
['attribute' => 'news_to_date', 'is' => new \Zend_Db_Expr('not null')],
]
)->addAttributeToSort(
'news_from_date',
'desc'
)->setPageSize(
$this->getProductsCount()
)->setCurPage(
1
);
//$priCount = count($collection);
//return $priCount;
//echo "<pre>"; print_r($collection->getData()); echo "</pre>"; die("hi");
return $collection;
}
protected function _beforeToHtml()
{
$this->setProductCollection($this->newproduct());
return parent::_beforeToHtml();
}
}
When i check the collection using print_r I am able to get the collecton but when I return the collection it give me error as below in postman:
URL: http://domain.com/rest/V1/custom/catalog/newproduct
{
"messages": {
"error": [
{
"code": 500,
"message": "Server internal error. See details in report api/1117659851420"
}
]
}
}
Please help me how to return collection.
error log:
"Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection\\Interceptor::getId() in \/vendor\/magento\/framework\/Reflection\/DataObjectProcessor.php:82\nStack trace:\n#0 \/vendor\/magento\/framework\/Webapi\/ServiceOutputProcessor.php(108): Magento\\Framework\\Reflection\\DataObjectProcessor->buildOutputDataArray(Object(Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection\\Interceptor), '\\\\Magento\\\\Catalo...')\n#1 \/vendor\/magento\/framework\/Webapi\/ServiceOutputProcessor.php(60): Magento\\Framework\\Webapi\\ServiceOutputProcessor->convertValue(Object(Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection\\Interceptor), '\\\\Magento\\\\Catalo...')\n#2 \/vendor\/magento\/module-webapi\/Controller\/Rest\/SynchronousRequestProcessor.php(99): Magento\\Framework\\Webapi\\ServiceOutputProcessor->process(Object(Magento\\Catalog\\Model\\ResourceM' in '\/vendor\/magento\/framework\/Reflection\/DataObjectProcessor.php' on line 82"