0

enter image description here

I am integrating a custom payment module. I need to get the API keys in my controller. If possible in JS file also.

My Controller where I need to get the config values.

<?php
namespace vendorName\moduleName\Controller\Index;

class GetPrice extends \Magento\Framework\App\Action\Action
{
    protected $customHelper;

    public function __construct( 
        \Magento\Framework\App\Action\Context $context,
        \Vendor\Module\Helper\Data $customHelper 
    ) {    
        $this->customHelper = $customHelper;
        parent::__construct($context);
    }

    public function execute(){
        echo $this->customHelper->getConfig("payment/title/cmc_api_key");
    }
}

system.xml

    <section id="payment" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label">
        <group id="mypay" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
            <label>MyPay</label>
                <field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">
                <label>Title</label>
            </field>

Thanks In Advance.

8
  • You can get configuration value like - magento.stackexchange.com/questions/87789/… Nov 14, 2019 at 7:32
  • Is that working or not? Nov 14, 2019 at 10:14
  • no It is not working :(
    – Garry
    Nov 14, 2019 at 10:17
  • Please share your system.xml which is used to create a configuration field. Nov 14, 2019 at 10:20
  • ` <section id="payment" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label"> <group id="mypay" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label"> <field id="title" showInDefault="1" showInStore="1"
    – Garry
    Nov 14, 2019 at 10:56

3 Answers 3

1

You can create one Helper and in that helper you can create one function there by using that function you can get config value by passing only key of your configuration everywhere in Blocks, Controllers and Model etc..

app/code/Vendor/Module/Helper/Data.php

<?php
namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

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

    public function getConfig($configPath)
    {
        return $this->scopeConfig->getValue(
            $configPath,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Now you just need to inject this Helper class in your Controller like this

namespace Vendor\Module\Controller\Index;

use \Magento\Framework\App\Action\Context;
use \Vendor\Module\Helper\Data as Helper;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_helper;

    public function __construct(
        Context $context,
        Helper $helper
    ){
        $this->_helper = $helper;
        parent::__construct($context);
    }

    public function execute(){
        echo $this->_helper->getConfig("sectionid/groupid/fieldid");
    }
}

And in phtml you can get config value like this..

$test = $this->helper('Vendor\Module\Helper\Data')->getConfig('sectionid/groupid/fieldid');

You can download reference module here. I've added code in this module how you can access config data in your JS file in this module.

Hope this will help you!

15
  • Any way for including key config in JS?
    – Garry
    Nov 14, 2019 at 7:44
  • I've to use this in ajax controller. Can i use it like this? public function __construct( \Magento\Framework\App\Action\Context $context, \Vendor\Module\Helper\Data $customHelper) { $this->customHelper = $customHelper; return parent::__construct($context, $this->customHelper); }
    – Garry
    Nov 14, 2019 at 7:56
  • You can use like this Nov 14, 2019 at 7:59
  • public function __construct( \Magento\Framework\App\Action\Context $context, \Vendor\Module\Helper\Data $customHelper ) { $this->customHelper = $customHelper; parent::__construct($context); } Nov 14, 2019 at 7:59
  • I am getting Error here when i request price with AJAX. jquery.js:10260 POST http://127.0.0.1/magento3/modulename/Index/GetPrice 500 (Internal Server Error) Here Is my section <section id="payment" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label"> Group <group id="mynewgroup" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label"> FieldID <field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">
    – Garry
    Nov 14, 2019 at 8:07
1

Get a value in your controller like, According to your system.xml you given wrong path, use $configPath variable path to get your configuration

<?php
namespace vendorName\moduleName\Controller\Index;

class GetPrice extends \Magento\Framework\App\Action\Action
{
    protected $scopeConfig;

    public function __construct( 
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {    
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    public function execute(){
        $configPath = 'payment/mypay/title';
        echo $this->scopeConfig->getValue(
                  $configPath,
                  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
             );
    }
}
0

To get value in JS file you can use the below code.

In your module etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Checkout\Model\CompositeConfigProvider">
       <arguments>
           <argument name="configProviders" xsi:type="array">
               <item name="api_key" xsi:type="object">Vendor\Module\Model\ApiKeyConfigProvider</item>
           </argument>
       </arguments>
   </type>
</config>

and in your ApiKeyConfigProvider.php add below code.

<?php

namespace Vendor\Module\Model;
class ApiKeyConfigProvider implements \Magento\Checkout\Model\ConfigProviderInterface
{
   public function getConfig()
   {
       $output['api_key'] = 'your_api_key';
       return $output;
   }
}

You can access above api key in js file like this.

window.checkoutConfig.api_key
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.