1

How can I create a plugin class to intercept the save product method, with an around method, but only when this is a request from API, is it possible? Thanks in advance.

[Edit]

My current structure: app/code/Vendor/Module/etc/webapi_rest/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="name-integrations" type="Vendor\Module\Plugin\ProductPlugin" sortOrder="10"/>
    </type>
</config>

And My ProductPlugin class:

<?php

namespace Vendor\Module\Plugin;

class ProductPlugin
{    
    public function aroundSave
    (
        \Magento\Catalog\Api\Data\ProductInterface $product, 
        $saveOptions = false, 
        \Closure $proceed
    ){
        // logging to test override    
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface');
        $logger->debug('Here: '.__METHOD__ . ' - ' . __LINE__);

        // call the core observed function
        $returnValue = $proceed(); 

        // logging to test override        
        $logger->debug(__METHOD__ . ' - ' . __LINE__);

        return $returnValue;
    }
}
?>

But When I execute this via API, I get this error: "message": "Internal Error. Details are available in Magento log file. Report ID: webapi-5aabfd61c4d51"

And in log I get:

[2018-03-16 17:22:41] main.CRITICAL: Report ID: webapi-5aabfd61c4d51; Message: Plugin class Gluk\Integracoes\Plugin\ProductPlugin doesn't exist {"exception":"[object] (Exception(code: 0): Report ID: webapi-5aabfd61c4d51; Message: Plugin class Gluk\Integracoes\Plugin\ProductPlugin doesn't exist at /opt/lampp/htdocs/marketplace/vendor/magento/framework/Webapi/ErrorProcessor.php:205, InvalidArgumentException(code: 0): Plugin class Gluk\Integracoes\Plugin\ProductPlugin doesn't exist at /opt/lampp/htdocs/marketplace/vendor/magento/framework/Interception/PluginList/PluginList.php:182)"} []

It is sayng that this plugin doesn't exists, and I cant see where is my mistake, I've already runned bin/magento setup:di:compile successful.

1

If you add a di.xml file in you module inside the folders etc/webapi_rest or etc/webapi_soap that file will be taken into account only when on api rest or api soap context.
So I guess you can write your plugin for the interface Magento\Catalog\Api\ProductRepositoryInterface for method save, but declare it in di.xml inside the folders mentioned above.

  • Thanks for answering, I've tryed like you sayd, but I got some errors, can you see my new edit please? – lpFranz Mar 16 '18 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.