2

I am trying to overwrite the customer save controller but it is not working.

This is my di.xml file app/code/Mp/Accountmanager/etc/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">
    <preference for="Magento\Customer\Controller\Account\EditPost" type="Mp\Accountmanager\Controller\Customer\EditPost" />
</config>

This is my Controller app/code/Mp/Accountmanager/Controller/Customer/EditPost.php

<?php
    namespace Mp\Accountmanager\Controller\Customer; 

class EditPost extends \Magento\Customer\Controller\Account\EditPost
    {
        /**
         * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
         */
        public function execute()
        {
            echo "yes";
            return parent::execute();
            exit;
        }
    }

This is not working when I edit and save the customer. Please help me.

  • How did you test this code? Searching for "yes" or by using debugger? I think that "yes" string is not visible because this controller is redirecting you to another page (controller) and your string is not displayed even if your code is executed. – George M Apr 13 '18 at 11:50
  • I change the customer account information and click on save. – Sunny Rahevar Apr 13 '18 at 11:58
  • Try to change echo "yes"; to $this->messageManager->addNoticeMessage(__('Yes'));. If "Yes" is displayed as notice in your page, your code is working. – George M Apr 13 '18 at 12:01
  • No, Message does not displaying after save the customer. – Sunny Rahevar Apr 13 '18 at 12:03
0

Seems that we need to move your di.xml under etc/frontend folder => etc/frontend/di.xml. It should work after moving. One more thing, avoiding overriding whole class, we should use Plugin as we can.

For example:

app/code/Mp/Accountmanager/etc/frontend/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\Controller\Account\EditPost">
        <plugin name="restrictEditInfo" type="Mp\Accountmanager\Plugin\Controller\Customer\EditPost" />
    </type>
</config>

app/code/Mp/Accountmanager/Plugin/Controller/Customer/EditPost.php

<?php

namespace Mp\Accountmanager\Plugin\Controller\Customer;


class EditPost
{

    /**
     * @param \Magento\Customer\Controller\Account\EditPost $subject
     * @param \Closure $proceed
     * @return mixed
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function aroundExecute(
        \Magento\Customer\Controller\Account\EditPost $subject,
        \Closure $proceed
    )
    {
        //Your logic...
        return $proceed();
    }
}
  • can you please send me the code with plugin? – Sunny Rahevar Apr 13 '18 at 11:59
  • There is already di.xml file in my frontend folder , so i have only copy this code ' <preference for="Magento\Customer\Controller\Account\EditPost" type="Mp\Accountmanager\Controller\Customer\EditPost" />' and paste in my existing di.xml file – Sunny Rahevar Apr 13 '18 at 12:04
  • See my updated answer. – Khoa TruongDinh Apr 13 '18 at 12:06
  • Please, careful with my typos :) – Khoa TruongDinh Apr 13 '18 at 12:08
  • not working..... is it mandatory to static content deploy? – Sunny Rahevar Apr 13 '18 at 12:12

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.