1

I would like to add extra information to the customer. I have added field in admin side form of customer. I have to save that data. How to add plugin around method during save customer.

  • Are you referring to save Customer Attribute ? – Vivek Apr 17 '18 at 5:25
  • Yes i have to save custom customer attribute. – Magecode Apr 17 '18 at 5:26
2

Check below answer.

Vendor/Module/etc/di.xml:

<type name="Magento\Customer\Controller\Adminhtml\Index\Save">
    <plugin name="save_fields" type="Vendor\Module\Model\Plugin\Customer" />    
</type>

Vendor/Module/Model/Plugin/Customer.php:

<?php

namespace Vendor\Module\Model\Plugin;

use Magento\Customer\Api\Data\GroupInterface;
use Magento\Framework\Api\ExtensionAttributesInterface\Config;

class Customer
{
    protected $_responseFactory;
    protected $_url;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_taxCustomer = $taxCustomer;
    }

    public function aroundexecute(\Magento\Customer\Controller\Adminhtml\Index\Save $save)
    {
        $post = $save->getRequest()->getPostValue();
        print_r($post); exit;
        // Do your stuff
    }
}

Hope it will help you.!!

  • I don't have to save customer attribute data from frontend. It will be only change by the Admin. So this solution will not work. – Magecode Apr 17 '18 at 5:35
  • So, You are looking for adminhtml execute around method via plugin ? – Vivek Apr 17 '18 at 5:36
  • Yes i'm looking for it. – Magecode Apr 17 '18 at 5:36
  • @Magecode, Check I've updated code. – Vivek Apr 17 '18 at 5:54
1

You can use this observer event to save additional custom attribute value for back-end.

  'adminhtml_customer_save_after',
['customer' => $customer, 'request' => $this->getRequest()]);

Creates the events.xml file under the etc folder.

<event name="adminhtml_customer_save_after">
        <observer name="customerSave" instance="VenderName\ModuleName\Observer\CustomerSaveAfter" />
</event>

Creates the observer file,

<?php

namespace VendorName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;
use \Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;

class CustomerSaveAfter implements ObserverInterface
{
    protected $customerRepository;

    public function __construct(
        CustomerRepositoryInterface $customerRepository,
        \Magento\Framework\App\RequestInterface $request
    ) {
        $this->_request = $request;
        $this->customerRepository = $customerRepository;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $data = $this->_request->getParams();
        //Write your save logic here
        $customAttribute = $data['custom_attribute'];
        $customer->setCustomAttribute($customAttribute);
        $this->customerRepository->save($customer);
    }
}
  • Can you please give this in brief? what is ['customer' => $customer, 'request' => $this->getRequest()]); – Magecode Apr 17 '18 at 5:55
  • I have edited my answer please check. – Emipro Technologies Pvt. Ltd. Apr 17 '18 at 6:14
  • Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Customer::save() – Magecode Apr 17 '18 at 6:58
  • I updated my answer. Please check it. – Emipro Technologies Pvt. Ltd. Apr 17 '18 at 7:15
  • I'm getting an error: PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Customer\Model\ResourceModel\CustomerRepository\Interceptor::save() must implement interface Magento\Customer\Api\Data\CustomerInterface – Kaushal Suthar Jul 11 '18 at 10:00

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.