我试图让所有网站都在自定义模板中使用。我在我的块中创建了以下内容;
public function getWebsites()
{
return $this->_storeManager->getWebsites();
}
然后我尝试使用这个迭代我的模板中的网站;
<?php foreach ($block->getWebsites() as $website): ?>
当页面尝试加载时,我得到;
警告:为 foreach() 提供的参数无效
我已经尝试了所有可能的变体,但似乎无法让它工作。它在众多论坛中被多次引用为检索所有网站的正确方法,但它对我不起作用。
如何在我的模板中获取一系列网站?
最佳答案
protected $_storeRepository;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreRepository $StoreRepository
) {
parent::__construct($context);
$this->_storeRepository = $StoreRepository;
}
public function getWebsite()
{
$stores = $this->_storeRepository->getList();
$websiteIds = array();
foreach ($stores as $store) {
$websiteId = $store["website_id"];
array_push($websiteIds, $websiteId);
}
return $websiteIds;
}
关于php - Magento 2 获取网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926348/