Latest Products API: Magento 2 Get Latest Products Using REST API Call

In this Wishusucess custom API development of module series, I am going to explain how you can get all the latest products of the Magento 2 website on other websites using the latest products API call.

Wishusucess Magento 2 expert developer has developed a module which is Wishusucess_LatestProductsApi that will help you to get load all your products just by calling the API.

 

Create Product Yes/No Attribute

first I have created a product yes no attributes in Admin > Stores > Attributes > Product.

Now there I have decided an identifier that is latestproducts, now I am going to use this identifier in the controller and model class of this custom Magento 2 Latest Products yes/no attributes model.

At the end of module, we can get all collections just by rest API call on any other website.

 

Step 1: Registration of LatestProductsApi

app/code/Wishusucess/LatestProductsApi/registration.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_LatestProductsApi',
__DIR__
);

 

Step 2: Module Basic Information

app/code/Wishusucess/LatestProductsApi/etc/module.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Wishusucess_LatestProductsApi" setup_version="1.0.0" />
</config>

 

Step 3: Declare Dependency of Module

app/code/Wishusucess/LatestProductsApi/etc/di.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Wishusucess\LatestProductsApi\Api\CustomInterface" type="Wishusucess\LatestProductsApi\Model\Api\Custom"/>
</config>

 

Step 4: Latest Products API Routing URL

Here, I have decided on a routing method GET and route URL, and also I have added a custom service class which I have implemented in our custom module.

app/code/Wishusucess/LatestProductsApi/etc/webapi.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route method="GET" url="/V1/wishusucess/latestproductsapi/">
<service class="Wishusucess\LatestProductsApi\Api\CustomInterface" method="getPost"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

 

Step 6: Routes URL ID

app/code/Wishusucess/LatestProductsApi/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="latestproductsapi" frontName="latestproductsapi">
<module name="Wishusucess_LatestProductsApi" />
</route>
</router>
</config>

 

Step 7: Controller Index File

Now, here I have added custom product attribute latestproducts in the controller class so that attribute filter the product based on 1 and 0 value of the attribute.

app/code/Wishusucess/LatestProductsApi/Controller/Index/Index.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\LatestProductsApi\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {

/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
protected $data;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory; 
$this->catalogProductVisibility = $catalogProductVisibility;
$this->data = $data;
parent::__construct($context);
}

public function execute() {
$collection = $this->_productCollectionFactory->create();
$collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->addAttributeToFilter('status', '1')
->addAttributeToFilter('latestproducts', '1');

$collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
$response = $collection;

echo "<pre>";
// print_r($response->getData());
$cart = array();
foreach($response as $value)
{
$cart[] = $value->getData();
}

print_r($cart);
die();

}

}

 

Step 8: Model Class Latest Products API

app/code/Wishusucess/LatestProductsApi/Model/Api/Custom.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\LatestProductsApi\Model\Api;

use Psr\Log\LoggerInterface;

class Custom 
{
/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;

public function __construct(
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
array $data = []
) {
$this->resultPageFactory = $resultPageFactory;
$this->_productCollectionFactory = $productCollectionFactory; 
$this->catalogProductVisibility = $catalogProductVisibility;


}

/**
* @inheritdoc
*/

public function getPost()
{
$response = ['success' => false];

try {

$collection = $this->_productCollectionFactory->create();
$collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->addAttributeToFilter('status', '1')
->addAttributeToFilter('latestproducts', '1');

$collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
$result= $collection;
$cart = array();
foreach($result as $value)
{
$cart[] = $value->getData();
}
$response= $cart;
// print_r($cart);


} catch (\Exception $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
}
$returnArray = $response;
return $returnArray; 
}
}

 

Step 9: Latest Products API Interface

app/code/Wishusucess/LatestProductsApi/Api/CustomInterface.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_LatestProductsApi
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\LatestProductsApi\Api;

interface CustomInterface
{
/**
* GET for Post api
* @return boolean|array
*/

public function getPost();
}

 

Now Run Bellow Command:

php/bin magento setup:upgrade

php/bin magento setup:di:compile

php/bin magento setup:static-content:deploy -f

php/bin magento c:c

 

Now, I am going to test this module using the postman API tool.

So, first I will create an admin access token by using the below Magento 2 admin API.

https://wishusucess.com/rest/store_id/V1/integration/admin/token

Magento 2 Admin Access Token

Now we have received the admin access token so we will use this token to authorize the API request.

So I am going to add this token to the header section.

Authorization: Bearer Token

Content-Type: application/JSON

Method: POST

Payload: Not Required

Authorise Your Request With Token

Now with the admin access token, I am using this implemented token.

https://wishusucess.com/rest/store_id/V1/sparx/latestproduct/

So all the enabled lasted product is loading here.

Call Latest Products API

Related Post:

How To GET Products List Using Magento 2 REST API

How To Get Product Image URL Using REST API in Magento 2

How To Get Category List By REST API and GraphQL In Magento 2, E.g.

 

Recommended Post:

Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04

Download Latest Product Custom Module on GitHub