首页 > 解决方案 > Magento 2如何以编程方式登录特定商店的用户

问题描述


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

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);

但它没有用。

标签: magento2magento2.2

解决方案


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


推荐阅读