0

Magento version - 2.1.9

Issue - We have a clothing store where we have 6 to 7 customer groups so what we are looking for is instaed of having same home page for every custom group we want to show a different home page.

I wanted to write a logic when session login, get customer group id and redirect path cmd page url but I don't in which file I override.

Does anyone have any ideas how to implement this?

Thanks in advance.

0

Magento/Cms/Controller/Index/Index.php this is home controller. So you need to modify here according to your logic. So use plugin for additional logic.

Try following way:

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\Cms\Controller\Index\Index">
        <plugin name="restrict_home_page" type="Vendor\Module\Plugin\Cms\Controller\Index\Index" sortOrder="1"/>
    </type>
</config>

Vendor/Module/Plugin/Cms/Controller/Index/Index.php


namespace Vendor\Module\Plugin\Cms\Controller\Index;

class Index
{
    /**
     * @var \Magento\Cms\Helper\Page
     */
    protected $pageHelper;

    /**
     * @var \Magento\Framework\Controller\Result\ForwardFactory
     */
    protected $resultForwardFactory;

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * Index constructor.
     * 
     * @param \Magento\Cms\Helper\Page $pageHelper
     * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
     * @param \Magento\Framework\App\Http\Context $httpContext
     */
    public function __construct(
        \Magento\Cms\Helper\Page $pageHelper,
        \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->pageHelper = $pageHelper;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->httpContext = $httpContext;
    }

    public function aroundExecute(
        \Magento\Cms\Controller\Index\Index $subject,
        \Closure $proceed,
        $coreRoute = null
    ) {
        $group = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP);
        $pageId = 0;
        if($group == 1) {
            $pageId = 4;
        } else if($group == 2) {
            $pageId = 2;
        }

        if($pageId) {
            $resultPage = $this->pageHelper->prepareResultPage($subject, $pageId);
            if (!$resultPage) {
                /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
                $resultForward = $this->resultForwardFactory->create();
                $resultForward->forward('defaultIndex');
                return $resultForward;
            }

            return $resultPage;
        } else {
            return $proceed($coreRoute);
        }
    }
}

Clear cache.

| improve this answer | |
  • Thanks! @shoel Rana. but it's not working for me. do you have any other thoughts or extensions that work for this functionality – abdul jani basha Shaik Sep 26 '17 at 14:32

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.