How To Get Store Config Value In Magento 2

In the previous article, we learned about How To Create Simple Module To Configure Options For Theme In Magento 2. Today, we will introduce you about How To Get Store Config Value In Magento 2.

I think you should read the first article: How To Create New Theme In Magento 2 in the series. It will help you understand the basics of Theme in Magento 2.

We’ll read the values directly from app/code/Magetop/Themes/etc/adminhtml/system.xml.

code config

The path config is section/group/value.

Get value yes/no in xml file

For example: Create file <theme_dir>/Magetop_Themes/layout/default.xml then insert the following code.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="page.top">
            <block ifconfig="themes/general/enable_topbanner" class="Magento\Framework\View\Element\Template"  name="top.banner" template="Magetop_Themes::html/mainBaner.phtml"/>
        </referenceContainer>
    </body>
</page>

Then create file <theme_dir>/Magetop_Themes/templates/html/mainBaner.phtml then insert the following code.

<h1>Create banner here</h1>

Use helper get theme config then call it in template file

For example: Create file app/code/Magetop/Themes/Helper/Data.php then insert the following code.

<?php
namespace Magetop\Themes\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($path='')
    {
        if($path) return $this->scopeConfig->getValue( $path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE );
        return $this->scopeConfig;
    }
}

Update file <theme_dir>/Magetop_Themes/layout/default.xml should look like:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="after.body.start">
            <block class="Magento\Framework\View\Element\Template" after="-"  name="themes.configure" template="Magetop_themes::html/themeConfigure.phtml"/>
        </referenceContainer>
        <referenceContainer name="page.top">
            <block ifconfig="themes/general/enable_topbanner" class="Magento\Framework\View\Element\Template"  name="top.banner" template="Magetop_themes::html/mainBaner.phtml"/>
        </referenceContainer>
    </body>
</page>

Then create file <theme_dir>/Magetop_Themes/templates/html/themeConfigure.phtml then insert the following code.

<?php 
    $cfg = $this->helper('Magetop\Themes\Helper\Data')->getConfig('themes/general/wellcome_text');
?>
<p><?php echo $cfg; ?></p>

Use Object Manager

But it isn’t a perfect choice, you should create simple module to customize your theme.

For example: Go to backend enable top banner and upload banner img then update file <theme_dir>/Magetop_Themes/templates/html/mainBaner.phtml should look like.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $bannerUrl = $objectManager
            ->get('Magento\Framework\App\Config\ScopeConfigInterface')
            ->getValue('themes/general/background_image');
    $bannerImgBg = $mediaUrl . 'themes/' . $bannerUrl;      
?>
<?php if($bannerUrl!=''): ?>
<div class="top_banner">
    <img src="<?php echo $bannerImgBg; ?>" alt="" />
</div>
<?php endif; ?>

We updated our file structure looks as follows

app/code/Magetop/
  ├── Themes/
  │   ├── Helper/
  │   │     ├── Data.php
  app/design/frontend/Magetop/
  ├── Default/
  │   ├── Magetop_Themes/
  │   │     ├── layout/
  │   │     │   ├── default.xml
  │   │     ├── templates/
  │   │     │   ├── html/
  │   │     │   │  ├── themeConfigure.phtml
  │   │     │   │  ├── mainBaner.phtml

Good luck!

This is How To Get Store Config Value In Magento 2

You can see the next post: How to make custom page layout in Magento 2.

Follow us for more helpful tutorial!

We hope this is useful blog for you.

Thank you for reading!

4.5 2 votes
Article Rating

Adam Roger

CEO and Founder of Magetop. A friend, a husband and a dad of two children. Adam loves to travel to experience new cultures and discover what is happening with ecommerce all around the world.

Leave a Reply or put your Question here

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x