Let’s initiate a discussion!!
In Magento 2, there are two types of template files. One is .phtml and second one is .html files under web/template directory.
In .phtml we easily get the config value, but it’s tricky to get config value in web template files which we are going to cover in this article.
You have to follow below steps :
Step 1 : Add below code to your web .js file:
1 |
app/code/[Vendor]/[ModuleName]/view/frontend/web/js/[DirectoryName]/[JsFileName].js |
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 31 |
define([ 'ko', 'jquery', 'uiComponent', 'mage/storage', 'mage/url' ], function (ko, $, Component, storage, url) { 'use strict'; return Component.extend({ defaults: { template: 'Vendor_ModueName/[DirectoryName]/[TemplateFileName]' }, getconfigSettingValue: function () { var serviceUrl = url.build('modulename/configsetting/settingfieldid'); storage.get(serviceUrl).done( function (response) { if (response.success) { return response.value } } ).fail( function (response) { return response.value } ); return false; } }); }); |
Step 2 : Create controller
1 |
app/code/[Vendor]/[ModuleName]/Controller/[DirectoryName]/ControllerFileName.php |
Add below code
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
namespace [Vendor]\[ModuleName]\Controller\[DirectoryName]; class ControllerFileName extends \Magento\Framework\App\Action\Action { protected $resultJsonFactory; protected $storeManager; protected $scopeConfig; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->resultJsonFactory = $resultJsonFactory; $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; parent::__construct($context); } /** * Execute view action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { $response = []; try { $configValue = $this->scopeConfig->getValue( 'your/path/config', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); $response = [ 'success' => true, 'value' => __($configValue) ]; } catch (\Exception $e) { $response = [ 'success' => false, 'value' => __($e->getMessage()) ]; $this->messageManager->addError($e->getMessage()); } $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData($response); } } |
Step 3 : Get config value as per below code in .html file.
1 |
<div class="config-setting-value" data-bind="html: getconfigSettingValue"></div> |
[crayon-641efb6cb0ddd053561623/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.