Adding a Plugin to call before customer API

sometimes when we need to modify the last name or first name after the API call to the website we may create a plugin to interrupt the call and modify the data before creating on the website

create a di.xml file inside etc in a vendor module
location vendor/module/etc/webapi_rest/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Api\AccountManagementInterface">
        <plugin name="Lastname" type="JJ\Customer\Plugin\Lastname"/>
    </type>
</config>

plugin location

Vendor/module/plugin

<?php
namespace JJ\Customer\Plugin;

use Magento\Customer\Api\Data\CustomerInterface;

class Lastname
{
    public function beforeCreateAccount(
        \Magento\Customer\Api\AccountManagementInterface $subject,
        CustomerInterface $customer,
        $redirectUrl = ''
    ){
        if($customer->getLastname() == null){
            $customer->setLastname('');
        }
        return [$customer,$redirectUrl];
    }
}

here done an example to modify the last name with a space.

Leave a comment

Your email address will not be published. Required fields are marked *