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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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.
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();
}
}
die
function.
– Max
Oct 6 '17 at 16:38
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;
}
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
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.
exit
function.
– Max
Oct 6 '17 at 16:38