60

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?

Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
Jorge
  • 5,610
  • 18
  • 47
  • 67

4 Answers4

152
$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);
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
  • 6
    Above answer is perfect. Just would like to add one thing. All the values specified in system.xml are stored in core_config_data table for example if you want the value of unsecure base url search the core_config_data for path="web/unsecure/base_url", you can get this value programatically by $configValue = Mage::getStoreConfig('web/secure/base_url'); – Harit Nov 20 '14 at 11:36
  • The fact that it even has to be searched is silly. convention over configuration. – ahnbizcad Sep 23 '16 at 01:06
  • Hi Mukesh. Do you know how to upload (and after that get in some block with Mage::StoreConfig) an image this way? I have an image in my form, set with image , but all I get on frontend (in a block), when I get it with ::StoreConfig is the last name ("Milka.jpg"), ie. not the full URL? – Vladimir Despotovic Nov 08 '16 at 22:07
  • What is the full qualified name of `Mage`? Is it `\Magento\Framework\View\Element\Context`? I don't seem to have any classes like that in my installation... – Klesun Jan 26 '20 at 18:59
22

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

  • 3
    I don't think so it is needed. The `getStoreConfig()` method looks like `return self::app()->getStore($store)->getConfig($path);`, so it get the same if you don't pass the second param. – deem Oct 23 '13 at 13:41
  • 1
    I would just like to point out that while your correct in most situations, there are cases where this doesn't work as expected and for peace of mind I would recommend adding the second store parameter. – Aaron Cole Feb 04 '14 at 00:31
  • for me, it didnt work without `Mage::app()->getStore()` with Magento v1.9.x – pHiL Jun 01 '16 at 12:32
21

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

Darren Felton
  • 2,299
  • 1
  • 18
  • 18
2

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);
ClaudioC
  • 1,505
  • 13
  • 10