Magento 2 如何以编程方式在特定商店中登录用户

Magento 2 how to login user in specific store programmatically


我如何在 Magento 2 中以编程方式在特定商店中登录客户?
我试图覆盖 execute 方法

Magento\Customer\Controller\Account\LoginPost

并做了这样的事情:

$this->storeManager->setCurrentStore("store_code");
$store = $this->storeRepository->getActiveStoreByCode("store_code");
$this->httpContext->setValue("store", "store_code", "default");
$this->storeCookieManager->setStoreCookie($store);

$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
$this->session->setCustomerDataAsLoggedIn($customer);

但是没有用。

没你想的那么难。 您不能设置 cookie 存储或类似的东西。 作为解决方法,您可以通过 ajax 查询 post 特定商店的凭据,如果响应成功 - 您可以将客户重定向到该商店。