我正在开发Magento 2。
但是无法找到在布局xml文件中获取scopeconfig值的解决方案。
在magento 1。
<block type="cms/block" ...>
<action method="..." ifconfig="config_path/config"></action>
</block>
在magento 2,如何使用"ifconfig"布局xml?
magento 1.x也是如此。
你可以在下面使用like。
<block class="MagentoFrameworkViewElementHtmlLinkCurrent" ifconfig="catalog/seo/search_terms" name="search-term-popular-link">
你可以直接使用like as
<block class="CedAbhinayBlockAccountActive" ifconfig="ced/account/activation" name="ced_account_activation">
,
Ced =您的Namespace
Abhinay = Your Module Name
Method1: Using Object Manager
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$conf = $objectManager
->get('MagentoFrameworkAppConfigScopeConfigInterface')
->getValue('section_id/group_id/field_id');
echo $conf;
?>
方法2:使用Helper
在模块的Helper文件夹中创建Data.php,并在其中编写以下代码。
<?php
namespace VendorNameModuleNameHelper;
class Data extends MagentoFrameworkAppHelperAbstractHelper
{
public function getConfig($config_path)
{
return $this->scopeConfig->getValue(
$config_path,
MagentoStoreModelScopeInterface::SCOPE_STORE
);
}
}
?>
你可以在你的php文件中调用这个助手,代码如下-
<?php
$value=$this->helper('MeghaMenuHelperData')->getConfig('section_id/group_id/field_id');
echo $value;
?>
可以像下面这样使用。
<block class="MagentoRssBlockFeeds" ifconfig="rss/config/active" name="head_rss">
您可以使用下面的代码直接将范围配置值写入php文件。
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$conf = $objectManager
->get('MagentoFrameworkAppConfigScopeConfigInterface')
->getValue('group/field/value');
在自定义模块的帮助器
中获取配置值的函数<?php
namespace VendorModuleHelper;
class Data extends MagentoFrameworkAppHelperAbstractHelper
{
public function getConfig($config_path)
{
return $this->scopeConfig->getValue(
$config_path,
MagentoStoreModelScopeInterface::SCOPE_STORE
);
}
}
然后你可以在任何php文件中获得调用这个函数的配置值。
$this->helper('VendorModuleHelperData')->getConfig('section/group/field');
注:请参考以下链接。https://magento.stackexchange.com/questions/84481/magento-2-how-to-get-the-extensions-configuration-values-in-the-phtml-files 强调文本