Answers:
您必须使用Magento/Store/Model/Information
该类并getStoreInformationObject()
为此调用方法。
您必须在自定义块中注入此类,然后才能在模板中使用该类。
protected $_storeInfo;
public function __construct(
....
\Magento\Store\Model\Information $storeInfo,
....
) {
...
$this->_storeInfo = $storeInfo;
....
}
然后创建一个自定义方法来检索电话号码:
public function getPhoneNumber()
{
return $this->_storeInfo->getStoreInformationObject(Store $store)->getPhone();
}
因此,您可以在模板中调用:
$block->getPhoneNumber();
您永远不要直接使用对象管理器(请参阅此处的原因:Magento 2:直接使用还是不使用ObjectManager?)
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento/Store/Model/Information');
$storeInfo = $storeInformation->getStoreInformationObject($store);
然后,您可以通过拨打以下电话来获取电话:
$phone = $storeInfo->getPhone();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento\Store\Model\Information');
$store = $objectManager->create('Magento\Store\Model\Store');
$storeInfo = $storeInformation->getStoreInformationObject($store);
$phone = $storeInfo->getPhone();
您需要\Magento\Framework\App\Config\ScopeConfigInterface
在块中注入的实例。
$protected $scopeConfig;
public function __construct(
....
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
....
) {
...
$this->scopeConfig = $scopeConfig;
....
}
然后创建方法 getStorePhone()
public function getStorePhone()
{
return $this->scopeConfig->getValue(
'general/store_information/phone',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
并调用您的模板 echo $block->getStorePhone()
上面的方法不起作用,所以我尝试了以下方式,它对我有用...
namespace Vendor\Module\Block;
class Contact extends \Magento\Framework\View\Element\Template
{
protected $_storeInfo;
protected $_storeManagerInterface;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Store\Model\Information $storeInfo,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
array $data = []
)
{
parent::__construct($context, $data);
$this->_storeInfo = $storeInfo;
$this->_storeManagerInterface = $storeManagerInterface;
}
public function getPhoneNumber()
{
return $this->_storeInfo->getStoreInformationObject($this->_storeManagerInterface->getStore(null))->getPhone();
}
}
在模板文件中
echo $block->getPhoneNumber();
上面的代码对我不起作用。我已经尝试了以下有效的代码。
class Sociallinks extends \Magento\Framework\View\Element\Template
{
protected $socialLinksHelper;
protected $objMgr;
protected $storeInfo;
protected $scopeConfig;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Addpeople\Websettings\Helper\Data $myModuleHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->_socialLinksHelper = $myModuleHelper;
$this->_objMgr = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $this->_objMgr->create('Magento\Store\Model\Information');
$store = $this->_objMgr->create('Magento\Store\Model\Store');
$this->_storeInfo = $storeInformation->getStoreInformationObject($store);
}
public function getPhoneNumber()
{
return $this->_storeInfo->getPhone();
}
}
模板文件
<?php echo $block->getPhoneNumber();?>