如何从 Magento 系统配置获取数据

发布于 2024-11-05 07:49:39 字数 85 浏览 1 评论 0原文

我只是想知道如何获取自定义模块的配置数据。可以从管理system->configuration设置配置以及如何在前端拉取它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捂风挽笑 2024-11-12 07:49:39
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');

sectionNamegroupNamefieldName 存在于模块的 etc/system.xml 文件中。

上面的代码将自动获取当前查看的商店的配置值。

如果您想获取除当前查看的商店之外的任何其他商店的配置值,则可以将商店 ID 指定为 getStoreConfig 函数的第二个参数,如下所示:

$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $store);
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');

sectionName, groupName and fieldName are present in etc/system.xml file of your module.

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:

$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $store);
如果没结果 2024-11-12 07:49:39

您应该使用以下代码

$configValue = Mage::getStoreConfig(
                   'sectionName/groupName/fieldName',
                   Mage::app()->getStore()
               ); 

Mage::app()->getStore() 这将在获取值中添加商店代码,以便您可以获得当前商店的正确配置值,这将避免错误的商店值因为 magento 也用于多个商店/视图,所以必须添加商店代码来获取 magento 中的任何内容。

如果我们配置了多个商店或多个视图,那么这将确保我们获取当前商店的值

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.

if we have more then one store or multiple views configured then this will insure that we are getting values for current store

梦回梦里 2024-11-12 07:49:39

Magento 1.x

(下面提供了 magento 2 示例)

sectionNamegroupNamefieldName 存在于 etc/system 中模块的.xml 文件。

PHP 语法:

Mage::getStoreConfig('sectionName/groupName/fieldName');

来自管理中的编辑器,例如 CMS 页面或静态块的内容;目录类别、目录产品等的描述/简短描述。

{{config path="sectionName/groupName/fieldName"}}

为了使“在编辑器内”方法发挥作用,字段值必须通过过滤器传递,以便解析出 {{ ... }} 内容。 Magento 开箱即用,将为类别和产品描述以及 CMS 页面和静态块执行此操作。但是,如果您在自己的自定义视图脚本中输出内容并希望解析这些变量,则可以这样做:

<?php
    $example = Mage::getModel('identifier/name')->load(1);
    $filter  = Mage::getModel('cms/template_filter');
    echo $filter->filter($example->getData('field'));
?>

identifier/name 替换为您正在加载的模型的适当值,以及您要输出的属性名称的 field ,其中可能包含需要解析的 {{ ... }} 出现。

Magento 2.x

来自任何扩展 \Magento\Framework\View\Element\AbstractBlock

$this->_scopeConfig->getValue('sectionName/groupName/fieldName');

任何其他 PHP 类:

如果该类(并且没有一个是父类)不要通过构造函数注入 \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...

将其注入到类中后,您现在可以使用上面为块类给出的相同语法示例来获取存储配置值。

请注意,修改任何类的 __construct() 参数列表后,您可能必须清除生成的类以及依赖项注入目录: var/ Generation & var/di

Magento 1.x

(magento 2 example provided below)

sectionName, groupName and fieldName are present in etc/system.xml file of the module.

PHP Syntax:

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.

{{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:

<?php
    $example = Mage::getModel('identifier/name')->load(1);
    $filter  = Mage::getModel('cms/template_filter');
    echo $filter->filter($example->getData('field'));
?>

Replacing identifier/name with the a appropriate values for the model you are loading, and field with the name of the attribute you want to output, which may contain {{ ... }} occurrences that need to be parsed out.

Magento 2.x

From any Block class that extends \Magento\Framework\View\Element\AbstractBlock

$this->_scopeConfig->getValue('sectionName/groupName/fieldName');

Any other PHP class:

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.

// ... 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

放肆 2024-11-12 07:49:39

例如,如果您想从配置->存储电子邮件地址获取电子邮件地址。
您可以指定您想要的商店地址:

$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);

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:

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文