Magento

How to get store config values by scope in Magento 2

Written by Mahesh Makwana

Apr 20

How to get store config values by scope in Magento 2

We used to store configuration to store some value which affects our store and provides functionality from the configuration. All Magento store settings are saved and stored in core_config table in Magento 2.

The below code show how to get store config values:

<?php

namespace Dolphin\GetStoreVal\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    protected $scopeConfig;
 
    XML_PATH_KEY = 'section_name/group_name/field_name';// add your path here

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function getConfigValue($field, $storeId = null)
    {
        return $this->scopeConfig->getValue($field, ScopeInterface::SCOPE_STORE, $storeId);
    }

    public function getFieldVal($storeId = null)
    {
        return $this->getConfigValue(self::XML_PATH_KEY, $storeId);
    }

}

You can change the SCOPE_STORE to following:

SCOPE_STORES    For  Stores
SCOPE_GROUPS    For  Groups
SCOPE_WEBSITES  For  Websites
SCOPE_GROUP     For  Group
SCOPE_WEBSITE   For  Website

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.

Written by Mahesh Makwana

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP