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):
将其添加到我的模板中(在
  • 标记之间);我没有得到任何输出,只有
  • 中的一个空格;我得到同样的错误;为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;
    }