1

I want to redirect the customer to cart page after login from anywhere ex.login from checkout page or customer account login.

Anyone know how to do this?

Please help me.

0

Step 1 : Create

Vendor/Module/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="customer_login">
        <observer name="customer_login_observer" instance="Vendor\Module\Observer\CustomerLogin" />
    </event>

</config>

Step 2 :

Create Vendor/Module/Observer/CustomerLogin.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    protected $_responseFactory;
    protected $_url;

    public function __construct(
        \Magento\Framework\View\Layout $layout,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
    )
    {   
        $this->_layout = $layout;
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /*$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl('checkout/cart');
        return $resultRedirect;*/
        $RedirectUrl = $this->_url->getUrl('checkout/cart');
        $this->_responseFactory->create()->setRedirect($RedirectUrl)->sendResponse();
        die();
    }
}
  • I'm using magento 2.1.8.it's not working.I'm getting 302 response from customer/ajax/login when I use your code. – Sneha Panchal Oct 6 '17 at 11:47
  • 2
    bad idea because your observer will interrupt application flow using die function. – Max Oct 6 '17 at 16:38
  • @SnehaPanchal its working with the same version – SagarPPanchal Sep 18 '18 at 6:11
0

For customization after-login redirect you need to add after plugin on \Magento\Customer\Controller\Account\LoginPost::execute where you should check is customer logged in and create custom redirect result.

Example

public function afterExecute(LoginPost $subject, ResultInterface $result)
{
    $isCustomerLoggedIn = $this->httpContext->getValue(Context::CONTEXT_AUTH);
    if ($isCustomerLoggedIn) {
        $result = $this->resultRedirectFactory->create()
            ->setPath('checkout/cart');
    }

    return $result;
}
  • This is not working on checkout page with ajax login method Also It is redirecting after customer create and we want to redirect after login to cart page from checkout page. – Sneha Panchal Oct 9 '17 at 6:19
  • yes, you also need to change behavior of Magento/Checkout/view/frontend/web/js/view/authentication.js component for redirect to custom url during login via checkout page – Max Oct 9 '17 at 7:48
  • Can I use something else for module development? Because we need to install the module on different instances. Thank You! – Sneha Panchal Oct 9 '17 at 10:20
  • i do not propose to change core files, you can use some techniques for js component customization's via your modules, such as mixins for example devdocs.magento.com/guides/v2.2/javascript-dev-guide/javascript/… – Max Oct 9 '17 at 10:28
0

Create events.xml from Module/etc/frontend/ folder and paste it below code.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">

    <event name="customer_login">
        <observer name="custom_customer_login" instance="Theme\VendorName\Observer\RedirectCustomerToLoginAtObserver" />
    </event>

</config>

And create RedirectCustomerToLoginAtObserver.php file from Module/Observer folder and paste it below code.

<?php

namespace Theme\VendorName\Observer;

class RedirectCustomerToLoginAtObserver implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $_response;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

        /**
    * @param \Magento\Framework\UrlInterface $url
    * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
         */
    public function __construct
    (
        \Magento\Framework\UrlInterface $url,
        \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
    ){
        $this->_storeManager = $storeManagerInterface;
        $this->_url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $storeObj = $this->_storeManager->getStore(1);
        $BaseURL = $storeObj->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
        $url = $BaseURL . 'customer/account/login';
        $this->_response->setRedirect($url)->sendResponse();
    }
}

Feel free to ask if any.

  • 1
    bad idea because your observer will interrupt application flow using exit function. – Max Oct 6 '17 at 16:38
  • It is not working for me :( – Sneha Panchal Oct 9 '17 at 10:21
  • @SnehaPanchal let me check – Bojjaiah Oct 9 '17 at 10:37

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.