How to get data from Magento configuration

I'm just wandering around how I can get configuration data for my custom module. The configuration can be configured from admin system->configuration and how to display it in the external interface?

+49
php magento
May 05 '11 at 3:50 a.m.
source share
4 answers
 $configValue = Mage::getStoreConfig('sectionName/groupName/fieldName'); 

sectionName , group_name and field_name are present in the etc / system.xml file of your module.

The above code will automatically select the configuration value of the currently viewed storage.

If you want to get the configuration value of any store other than the currently viewed store, you can specify the store ID as the second parameter of the getStoreConfig function, as shown below:

 $storeId = 2; // ID of the store you want to fetch the value of $configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $storeId); 
+133
May 05 '11 at 6:35
source share
β€” -

you should use the following code

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

Mage::app()->getStore() this will add the repository code in the sample values ​​so that you can get the correct configuration values ​​for the current repository. This will avoid incorrect storage values, since magento is also used for multiple repositories / views, so it must add the repository code to retrieve anything in Magento.

if we have more configured storage or multiple views, this ensures that we get the values ​​for the current storage

+21
Feb 19 '13 at 6:37
source share

Magento 1.x

(magento 2 example below)

sectionName , group_name , and file_name are present in the module’s etc / system.xml file.

PHP syntax:

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

In the administrator editor, for example, on the CMS page or in a static block; description / brief description of the catalog category, catalog product, etc.

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

To approach the work "Inside the editor", the field value must be passed through a filter for the content {{...}} to be disassembled. From this article, Magento will do this to describe categories and products, as well as CMS pages and static blocks. However, if you output the content in your own custom script view and want these variables to be parsed, you can do it like this:

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

Substitution of the identifier / name with the corresponding values ​​for the loaded model and the field with the name of the desired attribute for the output, which may contain {{...}} entries that need to be analyzed.

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 its parents) inserts \Magento\Framework\App\Config\ScopeConfigInterface through the constructor, you will 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... 

After you entered it in your class, now you can get the store configuration values ​​with the same syntax example that is given above for block classes.

Please note that after changing the list of parameters of the __construct () class, you may need to clear the created classes, as well as the dependency injection directory: var/generation and var/di

+12
Jun 29 '14 at 23:08
source share

for example, if you want to get EMAIL ADDRESS from config-> save email addresses. You can indicate from which store you need an 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); 
+2
Oct 14 '16 at 12:20
source share



All Articles