We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How To Get Store Config Values In Magento 2
To get a store config value in Magento 2, you can use the 1. Inject
First, you need to inject the
scopeConfig
object, which is available via dependency injection. Here's how you can do it.
1. Inject ScopeConfigInterface
in Your Class
First, you need to inject the ScopeConfigInterface
in your constructor. This is how you can do it:
<?php /** * Magekwik Software * * @category Magekwik * @package Magekwik_AjaxCall * @author Magekwik * @copyright Copyright (c) Magekwik Software Private Limited (https://Magekwik.com) * @license https://store.Magekwik.com/license.html */ namespace Vendor\Module\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; class Config { const NAME = 'section_id/group_id/field_id'; private ScopeConfigInterface $scopeConfigInterface; /** * @param ScopeConfigInterface $scopeConfigInterface */ public function __construct( ScopeConfigInterface $scopeConfigInterface ) { $this->scopeConfigInterface = $scopeConfigInterface; } public function isEnable() { return $this->scopeConfigInterface->getValue(self::NAME); } }
2. Explanation
ScopeConfigInterface
: This is the interface that provides methods to retrieve configuration values.$configPath
: This is the path to the config value you want to retrieve, usually in the formatsection/group/field
.SCOPE_STORE
: This defines the scope from which the config value is retrieved. Commonly used scopes are:SCOPE_STORE
: For store-specific configuration.SCOPE_WEBSITE
: For website-specific configuration.SCOPE_DEFAULT
: For the default configuration.