0

I want to change the landing page for a customer who has an active session to their customer/account page. I set up a conditional in aroundExecute() in app/code/MYNAME/MODULE/Plugin/CMS/Controller/Index.php

<?php
namespace MYNAME\MODULE\Plugin\Cms\Controller\Index;

use Magento\Customer\Model\Session;

class Index
{
    /**
     * @var Session
     */
    protected $session;

    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param Session $customerSession
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Session $customerSession
    ) {
        $this->objectManager = $objectManager;
        $this->session = $customerSession;
    }

    /**
     * Renders CMS Home page
     *
     * @param string|null $coreRoute
     * @return \Magento\Framework\Controller\Result\Forward
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundExecute(
        \Magento\Cms\Controller\Index\Index $subject,
        \Closure $proceed,
        $coreRoute = null
    ) {

        if ($this->session->isLoggedIn()) {
            $pageId = 5;
            return $this->objectManager->get('Magento\Cms\Helper\Page')->prepareResultPage($subject, $pageId);
        }
        return $proceed($coreRoute);
    }

}

It works great as long as you know the $pageId (Its randomly set to 5 above). However, I cannot find the pageId for the Customer Account page. I can't find anything in the Magento docs nor anywhere online. The examples I have found return pageId for every page I create but returns nothing for pages generated by Magento. What is the pageId? Or is there a better way to approach this problem?

0

The CMS Page helper is for pages defined within the CMS module itself in the backend. That is the pageID you are referring to is actually the page_id column within the database that Magento will look up to load data from.

If you just want to redirect the customer to their account page, it would be to redirect them to the appropriate page. Something similar was asked before: Redirect customer to a custom page after login.

In essence, you will need to adjust the response to redirect them to /customer/account. Whether you can do this with the $coreRoute variable that's there, or if you need to change it to a beforeExecute or afterExecute implementation I can't say.

EDIT: If you want to show the customer account content if they're logged in and not redirect them (a la Facebook's homepage), then you need to intercept the request before it's dispatched and dispatch the customer account request instead.

So you would have an observer that would observe the event controller_action_predispatch. The observer would have to evaluate the request to make sure that:

  1. The customer is logged in
  2. The user came from outside the current domain

Then in the observer, you'd evaluate the above conditions and if they're both true, update the event's request object with the correct module, controller and action.

For example:

<?php

namespace My\Module\Observer;

class AccountLandingPage implements \Magento\Framework\Event\ObserverInterface\ObserverInterface
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Framework\Event\ManagerInterface
     */
    protected $eventManager;

    /**
     * @var \Magento\Framework\DataObject\Factory
     */
    protected $objectFactory;

    /**
     * @var \Magento\Framework\App\Response\Http
     */
    protected $response;

    /**
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Framework\DataObject\Factory $objectFactory
     * @param \Magento\Framework\App\Response\Http $response
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\DataObject\Factory $objectFactory,
        \Magento\Framework\App\Response\Http $response,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->eventManager = $eventManager;
        $this->objectFactory = $objectFactory;
        $this->response = $response;
        $this->customerSession = $customerSession;
    }

    /**
     * Implement landing page logic
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $request = $observer->getEvent()->getRequest();
        if ($this->customerSession->isLoggedIn() && $this->isExternalReferrer($request)) {
            $request->setModuleName('customer')
                    ->setControllerName('account')
                    ->setActionName('index')
                    ->setDispatched(false);
            return;
        }
    }

    /**
     * Evaluate referrer if external from current site
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return void
     */
    protected function isExternalReferrer($request)
    {
        // TODO: Determine external referrer check
        //       Possibly with HTTP Referrer header?
    }
}
2
  • Thanks, Brett. But what I want to do is a little different. I want to change the content that is presented on the landing page at the root url. The index controller code above does that. Like how Facebook gives you a signup page when you first go there- but from then on you just see your account page.
    – weebsta
    Jan 16 '20 at 0:22
  • @weebsta I updated my answer to include a simple request force.
    – Brett
    Jan 16 '20 at 1:51

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.