{"id":446,"date":"2020-05-22T10:45:54","date_gmt":"2020-05-22T10:45:54","guid":{"rendered":"https:\/\/www.magetrend.com\/blog\/?p=446"},"modified":"2022-05-29T08:30:45","modified_gmt":"2022-05-29T08:30:45","slug":"magento-2-get-config-value","status":"publish","type":"post","link":"https:\/\/www.magetrend.com\/blog\/magento-2-get-config-value\/","title":{"rendered":"Magento 2: How to get config value"},"content":{"rendered":"\n<p>In Magento 2, configuration data is stored to core_config_data database table. The easiest way to get a configuration value from there, is using the following code snippet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$value = \\Magento\\Framework\\App\\ObjectManager::getInstance()\n    -&gt;get(<span class=\"highlight\">\\Magento\\Framework\\App\\Config\\ScopeConfigInterface<\/span>::class)\n    -&gt;getValue(\n        <span class=\"highlight\">'sections\/group\/field'<\/span>,\n        <span class=\"highlight\">\\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE,<\/span>\n    );<\/code><\/pre>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>It&#8217;s the most closest alternative to construction, which we had used in Magento 1: Mage::getStoreConfig(&#8216;section\/group\/field&#8217;). <\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_446&utm_campaign=pos1\"><img decoding=\"async\"  src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-pdf-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_446&utm_campaign=pos1\">The Most Popular PDF Designer for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_446&utm_campaign=pos1\">with user-friendly PDF editor to build your own PDF look<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_446&utm_campaign=pos1\">Read More<\/a> <\/div> <\/div>\n\n\n\n<p>However, it&#8217;s not the best way. According to&nbsp;<a href=\"https:\/\/devdocs.magento.com\/guides\/v2.3\/extension-dev-guide\/object-manager.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Magento 2 documentation<\/a>&nbsp;to use objectmanager is not recommend. You can still use it in a temporary code or while you&#8217;re debugging something, but if you want to do it right, you should use dependency injection method.<\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">A way to get a configuration value without objectmanager<\/h2>\n\n\n\n<p>To avoid an objectmanager, in Magento 2 we&#8217;re using dependency injection. Using this pattern, ScopeConfigInterface object is included via class constructor. Here is the simple example, how to get config value using dependency injection method<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"highlight\">public $scopeConfig;<\/span>\n\npublic function __construct(\n    . . .\n    <span class=\"highlight\">\\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig<\/span>\n    . . .\n) {\n    . . .\n    <span class=\"highlight\">$this-&gt;scopeConfig = $scopeConfig;<\/span>\n    . . .\n}<\/code><\/pre>\n\n\n\n<p>and then to get a config value<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function mySomethingSomething()\n{\n    . . .\n    $valueFromConfig = <span class=\"highlight\">$this-&gt;scopeConfig-&gt;getValue(\n        'section\/group\/field',\n        \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE,\n    );<\/span>\n    . . .\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Getting a config value by store id<\/h2>\n\n\n\n<p>As you probably noticed, Magento is multi store platform and in some cases you might want to get a value from configuration according to store. The method, which we used in the examples above <strong>getValue<\/strong>, by default returns global configuration values. To change that, we need to pass our store id<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function mySomethingSomething()\n{\n    . . .\n    $valueFromConfig = $this-&gt;scopeConfig-&gt;getValue(\n        'sections\/group\/field',\n        \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE,,\n        <span class=\"highlight\">$storeId<\/span>,\n    );\n    . . .\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">No changes in frontend<\/h3>\n\n\n\n<p>If you have added the objectmanager code snippet in .phtml template file and can&#8217;t see any difference in fronted, probably it because of cache. Try to clear it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In case, it isn&#8217;t a temporary code &#8211; use dependency injection. It&#8217;s not a rocket science!<\/p>\n\n\n\n<p>That&#8217;s all for this post, folks.  Let me know in the comments below, if you have some questions, or had some problems using the code snippets. I will be more than happy to append this post with your case.<\/p>\n\n\n\n<p>Happy coding!<\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_446&utm_campaign=pos2\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-email-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_446&utm_campaign=pos2\">Responsive Emails Templates for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_446&utm_campaign=pos2\">Our #1 bestseller. With user-friendly template editor!<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_446&utm_campaign=pos2\">Read More<\/a> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Magento 2, configuration data is stored to core_config_data database table. The easiest way to get a configuration value from there, is using the following code snippet. It&#8217;s the most &hellip; <\/p>\n<div class=\"grid-footer\"><a href=\"https:\/\/www.magetrend.com\/blog\/magento-2-get-config-value\/\" class=\"more-link\">Read More <i class=\"mticon-arrow\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/446"}],"collection":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":15,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":469,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions\/469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media\/459"}],"wp:attachment":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}