0

I am able to set and get config data in a field under General table with the test code. The Magento backend displays the field as the screenshot below shows. The issue is this field is blank, but supposed to display the value from the table. Please help me find what is missing in the field.

enter image description here

system.xml is as follows:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="pmm" translate="label" type="text" sortOrder="330" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>PMM</label>
            <tab>general</tab>
            <resource>Magento_Config::pmm</resource>
            <group id="currency_conversion" translate="label" type="text" sortOrder="1100" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Currency Conversion</label>
                <field id="usd_to_cad_conversion" translate="label" type="select" sortOrder="120" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                    <label>USD to CAD Exchange Rate</label> <!-- Custom label -->
                </field>  
            </group>
        </section>
    </system>
</config>

my module.xml is as follows:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Perfectmakeupmirrors_CustomShipping" >
        <sequence>
            <module name="Magento_Store"/>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_SalesRule"/>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>

Finally, the test code written to set and get the config data is below:

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// Initial code to Bootstrap Magento 2
use Magento\Framework\App\Bootstrap;
use public_html\testcode\Writeconfig;
include('app/bootstrap.php');

$bootstrap = Bootstrap::create(BP, $_SERVER); // Magento gets initialized at this point

$objectManager = $bootstrap->getObjectManager();

// Object to get the current store time in PST
$timezone = $objectManager->get('Magento\Framework\Stdlib\DateTime\TimezoneInterface');

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Initial code to Bootstrap Magento 2 ends here

/************t*************************************************************************************/
$date = $timezone->date();
// If one needs to diplay date in the formate of Ymd use 'Ymd'
$current_store_time = $date->format('Ymd H:i:s');

echo "<body>";
echo "**********************************************Magento Store Time : " . $current_store_time . "**********************************<br><br>";

// Instantiate class Write_Read_Config.
// Call function to set config data.
// Call function to get config data and display.
$configWriter = $objectManager->get('Magento\Framework\App\Config\Storage\WriterInterface');
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$scopeConfig = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface');

//for all websites
$websites = $storeManager->getWebsites();
$scope = "websites";
$value = 1.26;
foreach($websites as $website) {
    echo $website->getId() . "<br>";
    // Set config data. 
    $configWriter->save("/pmm/currency_conversion/usd_to_cad_conversion", $value, $scope, $website->getId());
}

// Display Config data.
echo "Current USD to CAD exchange rate is " . $scopeConfig->getValue("/pmm/currency_conversion/usd_to_cad_conversion", "websites");
echo "</body>";
7
  • This is cache issue
    – Sohel Rana
    Jul 22 at 0:52
  • The value shows up in the field when in Website and default store view, but not in default config view. Jul 22 at 5:51
  • Clearing cache doesn't help. Jul 22 at 5:55
  • You saved in the website scope, so can't see that value in the default scope, which is fine. If you want to see from default scope, then you need to assign in the default scope
    – Sohel Rana
    Jul 22 at 5:57
  • Yes, I want to see it from default scope. Please specify what change to be done in my code. thanks Jul 22 at 5:59
1

For default scope:

$configWriter->save("/pmm/currency_conversion/usd_to_cad_conversion", $value, 'default', 0);
4
  • Thanks, I think should make changes also where I get the value, like $scopeConfig->getValue("pmm/currency_conversion/usd_to_cad_conversion", "default"); Jul 22 at 7:08
  • No need to do that. $scopeConfig->getValue("pmm/currency_conversion/usd_to_cad_conversion") this will handle rest .
    – Sohel Rana
    Jul 22 at 8:34
  • Still not working..Not sure why Jul 22 at 8:37
  • I needed to build to get it fixed. Jul 22 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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