Question

Need to get Sort By List of Options category products list page using Rest API.

I have checked default REST API but can't get the same.

https://devdocs.magento.com/swagger/

Any have idea how to get Sort By List of Options of category products list page using Rest API.

Any help would be appreciated. Thanks.

Était-ce utile?

La solution

As I know, I think there are no any core magento API provide data of sort by option list. So, we need to create custom API for that.

Follow this below steps for that :

Step 1 : create registration.php file at app/code/RH/CustomAPI/registration.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'RH_CustomAPI',
    __DIR__
);

Step 2 : create module.php file at app/code/RH/CustomAPI/etc/module.php

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="RH_CustomAPI" schema_version="0.0.1" setup_version="0.0.1">
    </module>
</config>

Step 3 : Create the webapi.xml file in the app/code/RH/CustomAPI/etc folder and paste the below code:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="RH\CustomAPI\Api\GetSortOrderInterface" method="getSortOrderData"/>
        <resources>
            <resource ref="admin"/>
        </resources>
    </route>
</routes>

Step 4 : Create the di.xml file in app/code/RH/CustomAPI/etc/ for API Interface file. So, paste the below code:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="RH\CustomAPI\Api\GetSortOrderInterface" type="RH\CustomAPI\Model\GetSortOrder" />
</config>

Step 5 : Define getSortOrderData() method in the interface file. Create GetSortOrderInterface.php file in the app/code/RH/CustomAPI/Api/ folder and paste the below code

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace RH\CustomAPI\Api;

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

Step 6 : Define getSortOrderData() in model file. Create GetSortOrder.php file in the app/code/RH/CustomAPI/Model/ folder and paste the below code :

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace RH\CustomAPI\Model;

use RH\CustomAPI\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;
    }
}

Now, execute {base_url}/rest/V1/getsortorder url to run API.

Output :

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top