Question

I am trying to set the order history page as the default page when the customer is logged in.

When the customer login is successful, it redirects to customer/account/

Instead of this can we direct to sales/order/history/

So when the customer login happens, he can see order history page instead of the dashboard page.

Is something like that can be achieved? Can anyone help me on this issue, please?

Thanks

Was it helpful?

Solution

override account controller as like below.

Vendor\Module\etc\di.xml

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">       
   <preference for="Magento\Customer\Controller\Account\Index" type="Vendor\Module\Controller\Account\ExtendIndex" /> 
</config>

Then, Vendor\Module\Controller\Account\ExtendIndex.php

<?php
 namespace Vendor\Module\Controller\Account;
 class ExtendIndex extends \Magento\Customer\Controller\Account\Index
 {
   public function execute()
  {
    $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setPath('sales/order/history');
    return $resultRedirect;
  }
}

I hope this will help you!!

OTHER TIPS

Override the LoginPost controller and set the redirect to history controller in the plugin

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

<?xml version="1.0"?>
<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\LoginPost">
    <plugin name="vendor_module_loginpostplugin" type="\Vendor\Module\Plugin\LoginPostPlugin" sortOrder="1" />
  </type>
</config>

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

<?php

namespace Vendor\Module\Plugin;

class LoginPostPlugin
{

    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        $result->setPath('sales/order/history'); 
        return $result;
    }

}

Note:Code tested

You can change My Account link by plugin

Add below code to app/code/Vendor/Module/etc/di.xml

<type name="Magento\Customer\Model\Url">
    <plugin name="custom_links" type="Vendor\Module\Model\Url" sortOrder="1" />
</type>

app/code/Vendor/Module/Model/Url.php

<?php

namespace Vendor\Module\Model;

use Magento\Framework\UrlInterface;

class Url
{
    /**
     * @var UrlInterface
     */
    protected $urlBuilder;


    /**
     * @param UrlInterface $urlBuilder
     */
    public function __construct(
        UrlInterface $urlBuilder
    ) {
        $this->urlBuilder = $urlBuilder;
    }

    public function afterGetAccountUrl(\Magento\Customer\Model\Url $subject, $result)
    {
        return $this->urlBuilder->getUrl('sales/order/history');
    }
}
  1. events.xml file in app\code\Vendore\Module\etc\frontend folder
<?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_success_after_order" instance="Vendore\Module\Observer\CustomerLogin" />
    </event>
</config>
  1. Create CustomerLogin.php file in app\code\Vendore\Module\Observer folder
<?php

namespace Builderschoice\CustomModule\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Builderschoice\Company\Model\CompanyFactory;


class CustomerLogin implements ObserverInterface
{
/**
 * @var \Magento\Framework\App\ResponseFactory
 */
protected $responseFactory;

protected $webkulData;

protected $_companyBlock;

protected $_companyFactory;

protected $_storeManagerInterface;

/**
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param \Zend\Validator\Uri $uri
 * @param \Magento\Framework\App\ResponseFactory $responseFactory
 */
public function __construct(
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Webkul\Marketplace\Helper\Data $webkulData,
    \Builderschoice\Company\Block\Index\Index $companyBlock,
    \Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
    CompanyFactory $companyFactory
) {
    $this->responseFactory = $responseFactory;
    $this->webkulData = $webkulData;
    $this->_companyBlock = $companyBlock;
    $this->_storeManagerInterface = $storeManagerInterface;
    $this->_companyFactory = $companyFactory;
}
/**
 * Handler for 'customer_login' event.
 *
 * @param Observer $observer
 * @return void
 */
public function execute(Observer $observer)
{
    $event = $observer->getEvent();
    $customer = $event->getCustomer();

    $storeManager = $this->_storeManagerInterface->create();
    $baseurl=$storeManager->getStore()->getBaseUrl();
    $url = $baseurl.'sales/order/history';

    $resultRedirect = $this->responseFactory->create();

    $resultRedirect->setRedirect($url)->sendResponse('200');

    exit();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top