php - Magento 2 获取网站

标签 php magento2

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

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/

相关文章:

attributes - 我如何从 magento 2 的属性值中获取属性标签?

javascript - 将数据 id 从 php 循环传递到 jquery

php - 使用 Laravel Installer 安装 Laravel 4.1

php - MySQL LIKE 限制

amazon-ec2 - 如何使用 HTTPS 配置带有 Varnish Cache 的 Magento 2

rest - 如何在不包括祖 parent 的情况下获得产品所属的类别?

phpqrcode 库以字符串形式返回图像

php - 如何处理使用相同数据库的两个独立 Laravel 项目的迁移?

php - 列存在但抛出未找到列 : 1054 Unknown column 'so.event_id' in 'where clause' while inserting into sales_order

javascript - Knockout 'data-bind' 中的 'scope' 关键字