php 如何从 Magento 系统配置中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5892476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from Magento System Configuration
提问by Jorge
I just wandering on how I can get the configuration data for my custom module. The configuration can be set from the admin system->configuration
and how to pull it in frontend?
我只是想知道如何获取自定义模块的配置数据。可以从管理员设置配置system->configuration
以及如何在前端拉取它?
回答by Mukesh Chapagain
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');
sectionName, groupNameand fieldNameare present in etc/system.xmlfile of your module.
sectionName、groupName和fieldName存在于模块的etc/system.xml文件中。
The above code will automatically fetch config value of currently viewed store.
上面的代码会自动获取当前查看商店的配置值。
If you want to fetch config value of any other store than the currently viewed store then you can specify store ID as the second parameter to the getStoreConfig
function as below:
如果要获取当前查看的商店以外的任何其他商店的配置值,则可以将商店 ID 指定为getStoreConfig
函数的第二个参数,如下所示:
$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $store);
回答by Jorge
you should you use following code
你应该使用以下代码
$configValue = Mage::getStoreConfig(
'sectionName/groupName/fieldName',
Mage::app()->getStore()
);
Mage::app()->getStore()
this will add store code in fetch values so that you can get correct configuration values for current store this will avoid incorrect store's values because magento is also use for multiple store/views so must add store code to fetch anything in magento.
Mage::app()->getStore()
这将在获取值中添加商店代码,以便您可以获得当前商店的正确配置值这将避免错误的商店值,因为 magento 也用于多个商店/视图,因此必须添加商店代码以在 magento 中获取任何内容。
if we have more then one store or multiple views configured then this will insure that we are getting values for current store
如果我们配置了不止一个商店或多个视图,那么这将确保我们获得当前商店的值
回答by Darren Felton
Magento 1.x
Magento 1.x
(magento 2 example provided below)
(下面提供了 magento 2 示例)
sectionName, groupNameand fieldNameare present in etc/system.xml file of the module.
sectionName、groupName和fieldName存在于模块的 etc/system.xml 文件中。
PHP Syntax:
PHP 语法:
Mage::getStoreConfig('sectionName/groupName/fieldName');
From within an editor in the admin, such as the content of a CMS Page or Static Block; the description/short description of a Catalog Category, Catalog Product, etc.
从管理中的编辑器中,例如 CMS 页面或静态块的内容;目录类别、目录产品等的描述/简短描述。
{{config path="sectionName/groupName/fieldName"}}
For the "Within an editor" approach to work, the field value must be passed through a filter for the {{ ... }} contents to be parsed out. Out of the box, Magento will do this for Category and Product descriptions, as well as CMS Pages and Static Blocks. However, if you are outputting the content within your own custom view script and want these variables to be parsed out, you can do so like this:
要使“在编辑器内”工作,字段值必须通过过滤器才能解析出 {{ ... }} 内容。开箱即用,Magento 将为类别和产品描述以及 CMS 页面和静态块执行此操作。但是,如果您在自己的自定义视图脚本中输出内容并希望解析这些变量,您可以这样做:
<?php
$example = Mage::getModel('identifier/name')->load(1);
$filter = Mage::getModel('cms/template_filter');
echo $filter->filter($example->getData('field'));
?>
Replacing identifier/namewith the a appropriate values for the model you are loading, and fieldwith the name of the attribute you want to output, which may contain {{ ... }} occurrences that need to be parsed out.
将标识符/名称替换为您正在加载的模型的适当值,以及您要输出的属性名称的字段,其中可能包含需要解析出的 {{ ... }} 出现。
Magento 2.x
Magento 2.x
From any Block class that extends \Magento\Framework\View\Element\AbstractBlock
从任何扩展 \Magento\Framework\View\Element\AbstractBlock 的 Block 类
$this->_scopeConfig->getValue('sectionName/groupName/fieldName');
Any other PHP class:
任何其他 PHP 类:
If the class (and none of it's parent's) does not inject \Magento\Framework\App\Config\ScopeConfigInterface
via the constructor, you'll have to add it to your class.
如果该类(并且没有一个是父类的)没有\Magento\Framework\App\Config\ScopeConfigInterface
通过构造函数注入,则您必须将其添加到您的类中。
// ... Remaining class definition above...
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* Constructor
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
// ...any other injected classes the class depends on...
) {
$this->_scopeConfig = $scopeConfig;
// Remaining constructor logic...
}
// ...remaining class definition below...
Once you have injected it into your class, you can now fetch store configuration values with the same syntax example given above for block classes.
一旦你将它注入到你的类中,你现在可以使用上面为块类给出的相同语法示例获取存储配置值。
Note that after modifying any class's __construct() parameter list, you may have to clear your generated classes as well as dependency injection directory:var/generation
& var/di
请注意,在修改任何类的 __construct() 参数列表后,您可能需要清除生成的类以及依赖注入目录:var/generation
&var/di
回答by claudio
for example if you want to get EMAIL ADDRESS from config->store email addresses. You can specify from wich store you will want the address:
例如,如果您想从 config->store 电子邮件地址获取电子邮件地址。您可以从您想要地址的哪家商店指定:
$store=Mage::app()->getStore()->getStoreId();
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_general/name',$store);
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_general/email',$store);