Sorting Products: Magento 2 REST API For Sort By Product on List Page

This article will give an idea about how we can filter the product on the list page using Magento 2 REST API. So if you are looking for the sorting products module then this Wishusucess sort by position API helps you to get Sort By List of Options category products list page using Magento 2 Rest API.

Sorting Products REST API

Magento 2 default REST API does not provide this functionality to the users so you have to implement the module to get this product sorted.

There is no core REST API in Magento 2 for the sort by an option list. So, I am going to create a custom REST API in Magento 2o get this functionality on the category page.

 

Step To Create Sorting Products REST API

In order to provide better convenience to our customers on the Magento website, we try to implement many functionalities.

One of them is that we provide the option of sorting product to the customer on the category page or list page.

In this article, we will develop a rest API and through that, we will filter the product of the category page based on its price based on its name based on its position

Wishusucess/SortByPositionsApi/registration.php

Wishusucess/SortByPositionsApi/etc/module.xml

Wishusucess/SortByPositionsApi/etc/di.xml

Wishusucess/SortByPositionsApi/etc/webapi.xml

Wishusucess/SortByPositionsApi/Model/GetSortOrder.php

Wishusucess/SortByPositionsApi/Api/GetSortOrderInterface.php

 

Step 1: Registration of Sort By Positions REST Api

app/code/Wishusucess/SortByPositionsApi/registration.php

<?php
/**
* Category: Wishusucess Sort By Positions REST Api
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_SortByPositionsApi',
__DIR__
);

 

Step 2: SortByPositionsApi Module XML File

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

<?xml version="1.0"?>
<!--
/**
* Category: Wishusucess Sort By Positions REST Api
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Wishusucess_SortByPositionsApi" schema_version="0.0.1" setup_version="0.0.1">
</module>
</config>

 

Step 3: Sorting Products Dependency Injection

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

<?xml version="1.0"?>
<!--
/**
* Category: Wishusucess Sort By Positions REST Api
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Wishusucess\SortByPositionsApi\Api\GetSortOrderInterface" type="Wishusucess\SortByPositionsApi\Model\GetSortOrder" />
</config>

 

Step 4: Web API Routing of Sorting Products

Here we will decide the route path of our filter product model and how we will use that path to get the filter products.

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

<?xml version="1.0"?>
<!--
/**
* Category: Wishusucess Sort By Positions REST Api
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route url="/V1/getsortorder" method="GET">
<service class="Wishusucess\SortByPositionsApi\Api\GetSortOrderInterface" method="getSortOrderData"/>
<resources>
<resource ref="admin"/>
</resources>
</route>
</routes>

 

Step 5: Create Model Class of Sorting Products

This class decides the sort order data using the getSortOrderData() method and this method will process over the catalog product to filter all products based on the conditions using getAttributeUsedForSortByArray().

app/code/Wishusucess/SortByPositionsApi/Model/GetSortOrder.php

<?php
/**
* Category: Wishusucess Sort By Positions REST Api
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
namespace Wishusucess\SortByPositionsApi\Model;

use Wishusucess\SortByPositionsApi\Api\GetSortOrderInterface;

class GetSortOrder implements GetSortOrderInterface {
/**
* @var \Magento\Catalog\Model\Config
*/
private $catalogConfig;

/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param \Magento\Framework\Escaper $escaper
*/
public function __construct(
\Magento\Catalog\Model\Config $catalogConfig,
\Magento\Framework\Escaper $escaper
) {
$this->_catalogConfig = $catalogConfig;
$this->escaper = $escaper;
}

/**
* Return array of Sort By List of Options of category products list page
* 
* @return array
*/
public function getSortOrderData() {
$sortOrder = $this->_catalogConfig->getAttributeUsedForSortByArray();
$custom_array = [];
foreach ($sortOrder as $key => $value) {
$custom_array[] = $this->escaper->escapeHtml(__($value));
}
return $custom_array;
}
}

 

Step 6: Call Method of Sorting

app/code/Wishusucess/SortByPositionsApi/Api/GetSortOrderInterface.php

<?php
/**
* Category: Wishusucess Sort By Positions REST Api
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
namespace Wishusucess\SortByPositionsApi\Api;

interface GetSortOrderInterface {
/**
* Returns sort order list
*
* @api
* @return array
*/
public function getSortOrderData();
}

 

Now Run Below Command:


php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:clean

php bin/magento cache:flush

 


Suggested Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

 

Recommended Posts:


Customer Image: Upload Customer Profile Pic Image Module in Magento 2

Customer Attribute: How to Add Custom Customer Attribute in Magento 2