1

I am using 3 stores on my same website, how can I restrict access to customers to different stores? I would like that customers can only buy at a particular store, not at all. It's possible?

1
  • Yes, you need to customize it. I suggest you make customer attribute like store_id, Assign store id during registration from a particular store and also allow admin to change it. Than make plugin/observer on login to check customer's store and match store id. if store id mismatch just restrict from the login Jul 17, 2019 at 11:13

1 Answer 1

1

First follow the below image and create a website for each store. After add-website switcher instead of store switcher by using this link.

enter image description here

After adding the below files to your module.

app/code/VendoreName/ModuleName/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">
    <!-- For By Pass Customer Sesion when Switch Store -->
    <preference for="Magento\Store\Controller\Store\SwitchAction" type="VendoreName\ModuleName\Controller\Store\SwitchAction" />
</config>

app/code/VendoreName/ModuleName/Controller/Store

SwitchAction.php

<?php

declare (strict_types = 1);

namespace VendoreName\ModuleName\Controller\Store;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreIsInactiveException;
use Magento\Store\Model\StoreManagerInterface;

class SwitchAction extends \Magento\Store\Controller\Store\SwitchAction
{

    public function execute()
    {
        $targetStoreCode = $this->_request->getParam(StoreManagerInterface::PARAM_NAME);
        $fromStoreCode = $this->_request->getParam(
            '___from_store',
            $this->storeCookieManager->getStoreCodeFromCookie()
        );

        $requestedUrlToRedirect = $this->_redirect->getRedirectUrl();
        $redirectUrl = $requestedUrlToRedirect;

        $error = null;
        try {
            $fromStore = $this->storeRepository->get($fromStoreCode);
            $targetStore = $this->storeRepository->getActiveStoreByCode($targetStoreCode);
        } catch (StoreIsInactiveException $e) {
            $error = __('Requested store is inactive');
        } catch (NoSuchEntityException $e) {
            $error = __("The store that was requested wasn't found. Verify the store and try again.");
        }
        if ($error !== null) {
            $this->messageManager->addErrorMessage($error);
        }
        //else {
        //     $redirectUrl = $this->storeSwitcher->switch($fromStore, $targetStore, $requestedUrlToRedirect);
        //     $this->cookieManager->setCookieForStore($targetStore);
        // }

        $this->getResponse()->setRedirect($redirectUrl);
    }
}

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.