質問

Stores> Configuration> General> Store Information の店舗の電話番号設定をテーマに追加しようとしています。モジュールでは、これがgetValue()\Magento\Framework\App\Config\ScopeConfigInterfaceを使用して実行できると思いますが、テーマ内でこれを使用する方法を表示できません。これまでのところ私はdefault.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');
.

注: オブジェクトマネージャを直接使用することを回避します。テンプレートをきれいにしておくべきです。ブロックに設定を追加してください。@Raphaelの答えに従うべきです。

これをブロックで試してみて、多くの検索後に私のために働いています

$ ISENABLED=Magento \ Framework \ App \ ObjectManager :: getInstance()
- > get( 'magento¥Framework¥App¥config¥scopeconfiginterface') - > getValue( 'section / group / field');

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top