Welcome to Kishan Savaliya's Magento 2 Blog.In this blog post, I will explain how to get store config value in Magento 2. If you're working with Magento 2 then you know that Magento allows you to add config value storewise/website scopes and you can use that differently in stores and websites. Magento having lots of default configurations for different purpose, also we can create custom configuration based on requirements. If you don't know how to create new system configuration in Magento 2 then this article can be useful to you.
Magento stores config values in core_config_data table in database with config path and config value pair, so let's start how to get config value in Magento 2.
Go to your Magento 2 root directory and navigate inside your module and create Helper file here..
app/code/SK/ConfigValue/Helper/Data.php
Content for this file is..
<?php
/*
* @package SK\ConfigValue
* @author Kishan Savaliya <kishansavaliyakb@gmail.com>
*/
namespace SK\ConfigValue\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
public function getConfig($configPath)
{
return $this->scopeConfig->getValue(
$configPath,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
You can inject above helper file in any files and you can use getConfig() function everywhere, you just need to pass your config path as a param and it'll return value of your config.
If you want config value in phtml file then you just need to use below lines there..
<?php
/*
* @package SK\ConfigValue
* @author Kishan Savaliya <kishansavaliyakb@gmail.com>
*/
$helper = $this->helper(\SK\ConfigValue\Helper\Data::class);
$configPath = "sk_section/sk_group/sk_field";
echo $configValue = $helper->getConfig($configPath);
Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.
If you enjoyed this blog post, share it with friends!