Php Magento 2获取网站
我正在尝试让所有网站在自定义模板中使用。我在我的块中创建了以下内容:Php Magento 2获取网站,php,magento2,Php,Magento2,我正在尝试让所有网站在自定义模板中使用。我在我的块中创建了以下内容: public function getWebsites() { return $this->_storeManager->getWebsites(); } 然后,我尝试使用此工具在我的模板中迭代网站 <?php foreach ($block->getWebsites() as $website): ?> 当页面试图加载时,我得到 警告:为foreach()提供的参数无效 我已经尝试
public function getWebsites()
{
return $this->_storeManager->getWebsites();
}
然后,我尝试使用此工具在我的模板中迭代网站
<?php foreach ($block->getWebsites() as $website): ?>
当页面试图加载时,我得到
警告:为foreach()提供的参数无效
我已经尝试了所有可能的变体,但似乎无法让它工作。许多论坛多次引用它,认为它是检索所有网站的正确方法,但它对我不起作用
如何在模板中获取一系列网站?尝试
print\r($block->getWebsites())
查看其中的内容。有时我在调用其他函数时会看到bug。所以你可以试试:$websites=$block->getWebsites();foreach($websites as$website):
将其添加到我的模板中(在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;
}