如何在 Magento 中获取商店信息?

新手上路,请多包涵

在 Magento 中,我如何获取活跃的商店信息,例如商店名称、行号等?

原文由 Chirag 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 347
2 个回答

要从 Magento 的任何地方获取有关当前商店的信息,请使用:

 <?php
$store = Mage::app()->getStore();

这将为您提供一个 Mage_Core_Model_Store 对象,其中包含您需要的一些信息:

 <?php
$name = $store->getName();


至于您关于行号的其他问题,我不确定您的意思。如果您的意思是您想知道代码中的行号(例如,用于错误处理),请尝试:

 <?php
$line      = __LINE__;
$file      = __FILE__;
$class     = __CLASS__;
$method    = __METHOD__;
$namespace = __NAMESPACE__;

原文由 Joe Mastey 发布,翻译遵循 CC BY-SA 3.0 许可协议

对于 Magento 1:

你可以使用所有这些功能可以在类中找到 Mage_Core_Model_Store

文件:app/code/core/Mage/Core/Model/Store.php

存储数据

Mage::app()->getStore();

商店编号

Mage::app()->getStore()->getStoreId();

商店代码

Mage::app()->getStore()->getCode();

Magento 2 使用块

protected $storeManager;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
)
{
    $this->storeManager = $storeManager;
    parent::__construct($context, $data);
}

public function getStoreId()
{
    return $this->storeManager->getStore()->getId();
}

使用对象管理器

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

echo $storeManager->getStore()->getStoreId();

而已

原文由 Jyotishina Rajwani 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题