0

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 3

1

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>
0

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.

  1. 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>
  1. 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;
    }
}
  1. Compile, see the result and vote this answer :-)

  2. Credits and stuff:

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.