【问题标题】:Magento 2 get websitesMagento 2 获取网站
【发布时间】:2016-06-20 15:30:39
【问题描述】:

我正在尝试让所有网站都在自定义模板中使用。我在我的块中创建了以下内容;

public function getWebsites()
{
   return $this->_storeManager->getWebsites();
}

然后我尝试使用这个来迭代我的模板中的网站;

<?php foreach ($block->getWebsites() as $website): ?>

当页面尝试加载时,我得到了;

警告:为 foreach() 提供的参数无效

我已经尝试了所有可能的变体,但似乎无法使其正常工作。它在众多论坛中被多次引用为检索所有网站的正确方法,但它对我不起作用。

如何在我的模板中获取一系列网站?

【问题讨论】:

  • 试试print_r($block-&gt;getWebsites())看看里面有什么。
  • 有时我在其他地方调用函数时会看到错误。所以你可以试试:$websites = $block-&gt;getWebsites(); foreach($websites as $website):
  • 将此添加到我的模板中(在
  • 标签之间); getWebsites()); ?> 我没有得到任何输出,
  • .
  • 中只有一个空格
  • 也试过这个; getWebsites(); ?> 我得到同样的错误;为 foreach() 提供的参数无效
  • 能否请您发布您的块文件
  • 标签: php magento2


    【解决方案1】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签