Magento 2 – write and get config values by scope
An important, but less good documented feature of Magento 2 is how to write and get config values by scope. You will find tons of code samples on how do this globally. Sometime you need to do different settings for different stores programmatically. So here is how this works.
Magento 2 – write and get config values by scope
Magento saves all adminhtml settings in core_config table in your Magento database. There you can get values by its path, a string which indicates path to and a variable name. With this path, you can get or set values by Magento 2 core methods. For this you need to use:
- \Magento\Framework\App\Config\Storage\WriteInterface
to write config values to database - \Magento\Framework\App\Config\ScopeConfigInterface
to read config values from database
Here is how this work with sample code.
Write
The following sample code shows how to write store config values by scope:
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 26 27 28 29 30 | class WriteConfig { protected $_logger; protected $_storeManager; protected $_configWriter; public function __construct( \Psr\Log\LoggerInterface $logger, \Magento\Framework\App\Config\Storage\WriterInterface $configWriter, \Magento\Store\Model\StoreManagerInterface $storeManager ){ $this->_logger = $logger; $this->_configWriter = $configWriter; $this->_storeManager = $storeManager; } public function setConfig($value) { //for all websites $websites = $this->_storeManager->getWebsites(); $scope = "websites"; foreach($websites as $website) { echo $website->getId().":\n"; $this->_configWriter->save('my_section/something/configvaluename', $value, $scope, $website->getId()); } return $this; } } |
You just need to call setConfig() method with a given value. This method stores this value into a defined path for all websites. So it generates a new setting (line in core_config table) for each defined website. This is done by using third and fourth param on save method. You use a unique path, a value, a scope and the id of this scope. If you do not use scope, you will wirte the value to default (store id 0). You can store values to scopes “website” or “store“.
Read
Now it is time to read the data by store. You can do this with the following sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class ReadConfig { protected $_scopeConfig; public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ){ $this->_scopeConfig = $scopeConfig; parent::__construct($context); } public function getConfig() { return $this->_scopeConfig->getValue("my_section/something/configvaluename", "websites"); } } |
It is quite easy, you only need to use getValue() method and add a second param with scope (here we use website scope). This will return the stored value for the current website.
Conclusion
I showed you how to write and read config values by scope (other than default). If you are working on a multi-store shop you will need this to programmatically set and read config values by scope.