once we click on logout link, I need to be redirected to cart page /catalog page not home page,how can I proceed to do so.
Thank you
3 Answers
create "customer_account_logoutsuccess.xml" file in "vendorName/moduleName/view/frondend/layout"
folder.
write following code in this file.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="customer_logout" template="vendorName_moduleName::logout.phtml"/>
</referenceContainer>
</body>
</page>
create logout.phtml file in "vendorName/moduleName/view/frontend/templates"
<p><?= $block->escapeHtml(__('You have signed out and will go to our homepage in 5 seconds.')) ?></p>
<script type="text/x-magento-init">
{
"*": {
"Magento_Customer/js/logout-redirect": {
"url": "<?= $block->escapeJs($block->escapeUrl($block->getUrl('checkout/cart'))) ?>"
}
}
}
</script>
A different approach would be to use an observer on logout and redirect from there.
However, there is a logic issue with your request. Redirecting to cart page after logout is not desirable since your session is destroyed and there will be no items left in your cart. Feel free to replace the redirect url with any other page.
- Create an events.xml file in Vendor/Module/etc/
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_postdispatch_customer_account_logout">
<observer name="Vendor_Module_Logout_Redirect" instance="Vendor\Module\Observer\Logout" />
</event>
</config>
- Create an observer file in Vendor/Module/Observer/
<?php
namespace Vendor\Module\Observer;
class Logout implements \Magento\Framework\Event\ObserverInterface
{
protected \Magento\Framework\App\ResponseFactory $responseFactory;
protected \Magento\Framework\UrlInterface $url;
public function __construct(
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\UrlInterface $url
){
$this->responseFactory = $responseFactory;
$this->url = $url;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$cartPageUrl = $this->url->getUrl('checkout/cart/index');
$this->responseFactory->create()->setRedirect($homepageUrl)->sendResponse();
return $this;
}
}
Compile, see the result and vote this answer :-)
Credits and stuff:
Override logout.phtml
file in your theme
app/design/frontend///Magento_Customer/templates/logout.phtml
logout.phtml file code
<p><?= $block->escapeHtml(__('You have signed out and will go to our homepage in 5 seconds.')) ?></p>
<script>
require(["jquery", 'jquery/jquery-storageapi'], function($){
$.localStorage.set('redirected', false);
});
</script>
<script type="text/x-magento-init">
{
"*": {
"Magento_Customer/js/logout-redirect": {
"url": "<?= $block->escapeJs($block->escapeUrl($block->getUrl('checkout/cart'))) ?>"
}
}
}
</script>