Magento 2 – How to Extend Core Function Only for REST API

magento2rest api

I want to extend some core function via the plugin and it should work in only when function call via REST API so in short my override things are only for REST API scope not for the web.

is this possible in Magento 2 and how can I write a plugin using di.xml which only call when request form API

Any idea? any help appreciate

Best Answer

You need to create plugin in di.xml inside etc/webapi_rest folder to call plugin only in REST API.

Example :

app/code/Vendor/Module/etc/webapi_rest/di.xml :

<?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="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Api\ProductCustomOptionRepositoryInterface">
        <plugin name="updateProductCustomOptionsAttributes" type="Magento\Catalog\Plugin\Model\Product\Option\UpdateProductCustomOptionsAttributes"/>
    </type>
</config>

Add your plugin based on your requirement in di.xml.

Related Topic