我正在尝试以编程方式切换商店。我使用以下代码来实现它:
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
接着:
$this->_storeManager->setCurrentStore('YOUR_STORE_ID');
如https://magento.stackexchange.com/a/173763/59686中给出的
但没有成功。店面仅显示(选中)默认商店。
我也尝试过这个 url 方案http://mystoreurl.com/?___store=storeId但它只显示具有给定 id 的商店而不是完全切换商店,意味着当我访问主 url (http:mystoreurl.com) 时,它再次显示默认商店。有没有办法以编程方式切换商店,就像从管理员那里选择默认一样。
或者有什么方法可以添加一些现成的小部件来切换商店(Store Switcher)。我使用的主题没有这个功能来自动填充商店切换器,因为默认的 Magento Luma 主题提供。