我正在尝试将商店的电话号码设置添加到 商店>配置>常规>常规>商店信息 到我的主题的标题。在一个模块中,我认为这可以使用 getValue()\Magento\Framework\App\Config\ScopeConfigInterface 但我看不到如何在主题中使用它的方法。到目前为止,我已经将此添加到默认值。xml

 <referenceContainer name="header-wrapper">
      <block class="Magento\Framework\View\Element\Template" name="store.phone.number" template="Magento_Theme::phone.phtml" />
 </referenceContainer>

但我不知道如何获取里面的电话号码 phone.phtml

有帮助吗?

解决方案

我建议你创建自己的块,这将扩展 Magento\Framework\View\Element\Template 类。

作为 Magento\Framework\App\Config\ScopeConfigInterface 是......的一部分 Magento\Framework\View\Element\AbstractBlock (家长的 Template 类)声明于 $_scopeConfig, ,您可以将以下功能添加到您的自定义块:

public function getConfig()
{
    return $this->_scopeConfig;
}

然后在你的模板中你可以做:

$block->getConfig()->getValue('value/you/need');

不要忘记像这样更新你的布局:

<referenceContainer name="header-wrapper">
      <block class="Vendor\Module\Block\View\Element\Template" name="store.phone.number" template="Magento_Theme::phone.phtml" />
</referenceContainer>

其他提示

我们可以通过获取 Magento\Framework\App\Config\ScopeConfig:

 \Magento\Framework\App\ObjectManager::getInstance()
  ->get('Magento\Framework\App\Config\ScopeConfigInterface')
  ->getValue('value/you/need');

从技术上讲,当有人要求一个 Magento\Framework\App\Config\ScopeConfigInterface, ,我们将给它一个实例 Magento\Framework\App\Config\ScopeConfig.例如,我们可以获取网格或列表模式的默认设置:

$productListMode = \Magento\Framework\App\ObjectManager::getInstance()
   ->get('Magento\Framework\App\Config\ScopeConfigInterface')
   ->getValue('catalog/frontend/list_mode');

注: 避免直接使用对象管理器.我们应该保持模板的清洁。尝试将config添加到块中。应该遵循@Raphael的答案。

在块上试试这个,经过多次搜索,它对我有效

$isEnabled=\Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('section/group/field');

许可以下: CC-BY-SA归因
scroll top