Featured Product API : Get Featured Product Using Magento 2 REST API

Here, we have created a custom module which is Featured Products API that will help you to get the product collection based on the product attribute.

Featured Product API is a custom module of the Wishusucess Magento developer team, it's an open-source module of wishusucess development team.

So, Let's start the step to create this custom REST API to get the product collection on another website by using the API call.

Featured Products API in Magento 2

 

Steps To Create Featured Products API

app/code/Wishusucess/FeaturProductsApi/registration.php

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

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

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

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

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

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

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

 

Step 1: Registration FeaturProductsApi

app/code/Wishusucess/FeaturProductsApi/registration.php

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

 

Step 2: Module Information of FeaturProductsApi

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

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_FeaturProductsApi
* 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_FeaturProductsApi" setup_version="1.0.0" />
</config>

 

Step 3: Define Dependency Injection

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

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_FeaturProductsApi
* 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\FeaturProductsApi\Api\CustomInterface" type="Wishusucess\FeaturProductsApi\Model\Api\Custom"/>
</config>

 

Step 4: Create Featured Products API

In this file of Web API, we have to decide the route ID that we have to call in order to get this collection of products on the third-party website or another website.

So, here we have decided the route id which is /V1/wishusucess/featureproducts and the method is GET.

So when we call this method then we have to define this GET method there then only we can get this product collection on that server.

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

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_FeaturProductsApi
* 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/featureproducts/">
<service class="Wishusucess\FeaturProductsApi\Api\CustomInterface" method="getPost"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

 

Step 5: Create Frontend Route File

By using this route.xml file we have decided the route id for the frontend which is featureproducts so this will id we will use in the frontend URL.

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

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_FeaturProductsApi
* 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="featureproducts" frontName="featureproducts">
<module name="Wishusucess_FeaturProductsApi" />
</route>
</router>
</config>

 

Step 6: Featured Products Api Model Class

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

<?php
/**
*
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_FeaturProductsApi
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\FeaturProductsApi\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('featured', '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 7: FeaturProductsApi Controller

This model class collection will filter the product based on the yes-no featured attributes.

So you can create this product attribute in the Magento 2 admin and then add that identifier here.

addAttributeToFilter('featured', '1');

Here featured is a product attribute identifier that will have boolean values yes means 1 and no means 0.

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

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

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

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;
parent::__construct(
$context,
$data
);
}

public function execute() {
$collection = $this->_productCollectionFactory->create();
$collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->addAttributeToFilter('status', '1')
->addAttributeToFilter('featured', '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:

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

<?php
/**
*
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_FeaturProductsApi
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\FeaturProductsApi\Api;

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

public function getPost();
}

 

Related Post:

How To Call Magento 2 REST API For Customer Account Update

How To GET Products List Using Magento 2 REST API

 

Download This API