テーマテンプレート内にStore Configを取得する方法
-
29-09-2020 - |
質問
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');