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> |
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62837cbd7f60d113492001/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...