0

I need to redirect to checkout from an observer related to the event customer_account_edited.

Here is my code. I've deleted a lot of rows that actually works...

Magento is doing everything well but ignore my redirection and send the user to the customer dashboard.

namespace Vendor\Customer\Observer;
use Magento\Customer\Api\CustomerRepositoryInterface;

class CustomerAccountEdited implements \Magento\Framework\Event\ObserverInterface {

protected $customerRepository;
protected $helper;
protected $_objectManager = null;
protected $resultRedirectFactory;

protected $messageManager;


public function __construct(
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Framework\ObjectManagerInterface $objectManager,
    CustomerRepositoryInterface $customerRepository,
    \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    \Vendor\Customer\Helper\Data $helper
){
    $this->_request             = $request;
    $this->customerRepository   = $customerRepository;
    $this->helper               = $helper;
    $this->_objectManager = $objectManager;
    $this->resultRedirectFactory = $resultRedirectFactory;
    $this->messageManager               = $messageManager;

}

public function execute( \Magento\Framework\Event\Observer $observer ){

    $this->messageManager->addSuccess(__("Success message"));
    $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setPath('checkout/cart');
    return $resultRedirect;

}
}
0

You can use this code in your Observer

<?php

namespace Vendor\Customer\Observer;

use Magento\Customer\Api\CustomerRepositoryInterface;

class CustomerAccountEdited implements \Magento\Framework\Event\ObserverInterface
{
    protected $customerRepository;
    protected $helper;
    protected $_objectManager = null;
    protected $resultRedirectFactory;
    protected $messageManager;
    protected $_redirect;
    protected $_actionFlag;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        CustomerRepositoryInterface $customerRepository,
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Vendor\Customer\Helper\Data $helper,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\App\ActionFlag $actionFlag
    ){
        $this->_request             = $request;
        $this->customerRepository   = $customerRepository;
        $this->helper               = $helper;
        $this->_objectManager       = $objectManager;
        $this->resultRedirectFactory= $resultRedirectFactory;
        $this->messageManager       = $messageManager;
        $this->_redirect = $redirect;
        $this->_actionFlag = $actionFlag;
    }

    public function execute( \Magento\Framework\Event\Observer $observer ){
        $controller = $observer->getControllerAction();
        $this->messageManager->addSuccess(
            __("Success message")
        );
        $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        $this->_redirect->redirect($controller->getResponse(), 'checkout/cart');
        return $observer;
    }
}

Hope this will helpful to you!

  • Did you tried this solution ? and is this working or not ? – Kishan Savaliya Oct 22 '19 at 11:12
0

Easily has done by Override the EditPost controller and set the redirect to checkout controller in the plugin

Filepath: Vendor/Module/etc/frontend/di.xml:

<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="vendor_module_editpost" type="\Vendor\Module\Plugin\EditPost" sortOrder="1" />
  </type>
</config>

Filepath: Vendor/Module/Plugin/EditPost.php:

<?php

namespace Vendor\Module\Plugin;

class EditPost
{

    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        $result->setPath('checkout'); // Change this to wha t you want
        return $result;
    }

}

Note:Code tested

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.