How to Get Store Config Value by Scope in Magento 2
Scope in Magento 2 allows setting the configuration based on a specific website, store or view. A bigger business leads to having multiple stores, websites and store views. Sometimes, it’s required to get values from the various scope and use it somewhere while creating an extension. This requires to get store config value by scope in Magento 2.
All the adminhtml settings are saved in core_config table of your Magento 2 database. You just need to call getConfig() method and add a second param with the scope to get the stored value for website, store or the store view.
Method to get store config value by scope in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace Vendor\Extension\Helper use Magento\Store\Model\ScopeInterface; use Magento\Framework\App\Helper\AbstractHelper; class Data extends AbstractHelper{ public function getConfig($scope) { try { switch($scope){ case 'store': return $this->scopeConfig->getValue('config/to/path', ScopeInterface::SCOPE_STORE); // From store view case 'website': return $this->scopeConfig->getValue('config/to/path', ScopeInterface::SCOPE_WEBSITE); // From Website default: return $this->scopeConfig->getValue('config/to/path', ScopeInterface::SCOPE_STORE); } } catch (\Exception $e) { return false; } } } ?> |
Using the above code, you can easily get store config value by scope in Magento 2. Remove the code with the scope as per your requirement. Do comment your doubts, suggestions, feedback, and queries. I would really appreciate your inputs and help you as soon as possible.
Don’t forget to hit the 5 stars below to reward my write-ups. 🙂
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Magento 2 Shipping Rules – Meetanshi Extension Explained
Simplified Solution to Magento 2 Backend Not Working
Next