5

To create a table configuration in Magento2 backend, we can use some good tutorial in here: http://www.ibnab.com/en/blog/magento-2/magento-2-backend-guide-to-create-table-setting-in-the-system-configuration or https://magently.com/blog/magento-2-backend-configuration-frontend-model-part-33/

I read this tutorial in Magento 1 https://www.integer-net.com/how-to-create-tables-in-magento-system-configuration/.

They created a table configuration with default value. I knew that in Magento 1, we can set default array value for table in config.xml like that:

<default>
    <namespace_module>
        <settings>
            <shipping_costs>a:1:{s:18:"_1425462208957_957";a:2:{s:10:"from_price";s:1:"0";s:4:"cost";s:4:"4.90";}}</shipping_costs>
        </setting>
    </namespace_module>
</default>

I try to put the same array above from Magento 1 to config.xml file on Magento 2, but it does not work at all.

Here is my example:

system.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="media" translate="label" sortOrder="300">
        <label>MEDIA</label>
    </tab>
    <section id="payment" translate="label" type="text" sortOrder="402" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Settings</label>
        <tab>media</tab>            
        <group id="setting" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <field id="interest_rates" translate="label comment" sortOrder="13" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Interest Rates</label>
                <frontend_model>Company\Module\Block\Adminhtml\System\Config\Fieldset\InterestRates</frontend_model>
                <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>                    
            </field>
        </group>
    </section>  
<system>    

File InterestRates

protected function _prepareToRender()
{
    $this->addColumn('duration', ['label' => __('Duration'), 'style' => 'width:60px']);
    $this->addColumn('effective_interest_rate', ['label' => __('Effective Interest Rate'), 'style' => 'width:170px']);
    $this->addColumn('special_effective_interest_rate', ['label' => __('Special Effective Interest Rate'), 'style' => 'width:170px']);
    $this->_addAfter = false;
    $this->_addButtonLabel = __('Add Interest Rate');
}

Picture demo enter image description here

I want to set default value for text column Duration, Effective_Interest_Rate and Special Effective Interest Rate

Can someone give me an advice? How can I set default value for table configuration in Magento .

2
  • Please share your system.xml file to get proper response. Jan 13, 2017 at 10:09
  • Hi I updated my questions, I need to set default value for text column above Jan 13, 2017 at 10:34

3 Answers 3

3

In your case, you can follow the same Magento 1 way, you have to enter a serialized array there. From my experience, the best way to generate that serialized array is to fill in the default data in the backend, save it and then copy the value from core_config_data table to the config.xml.

For example config xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <setting>
                <interest_rates>a:4:{s:18:"_1477023122716_716";a:3:{s:4:"from";s:1:"0";s:2:"to";s:5:"10000";s:3:"fee";s:3:"324";}s:18:"_1477023174715_715";a:3:{s:4:"from";s:5:"10001";s:2:"to";s:5:"30000";s:3:"fee";s:3:"432";}s:18:"_1477023192619_619";a:3:{s:4:"from";s:5:"30001";s:2:"to";s:6:"100000";s:3:"fee";s:3:"648";}s:18:"_1477023219483_483";a:3:{s:4:"from";s:6:"100001";s:2:"to";s:6:"300000";s:3:"fee";s:4:"1080";}}</interest_rates>
            </setting>
        </payment>
    </default>
</config>

We can use this site http://www.unserialize.com/ to check our serialized array.

4
  • Thanks a lot. I just got value from table core_config_data and it works really well Jan 17, 2017 at 10:56
  • It works, But after setting default config value, it still shows the fields as editable, Ho do I disable the fields while default value is set ?
    – Jaisa
    Aug 9, 2018 at 8:29
  • After module install, I see that config value are display in admin side. But when I get that config then I get error. I need to save it and then able to get it. config.xml save data in core_config_data or not? Dec 19, 2019 at 10:40
  • @Khoa this is helpfull for me Jan 20, 2020 at 15:11
0

Below code for set yes value to dropdown file Yes/No.

In config.xml file,

<?xml version="1.0"?>


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <setting>
                <interest_rates>100</interest_rates>
            </setting>
        </payment>
    </default>
</config>
7
  • Hi Rakesh, can you explain please? Why do we set only <enable>1</enable>. I want to set default value for text column in table configuration Jan 13, 2017 at 10:10
  • This is example for only Yes/No value, you can set text value as well but you have to define system.xml code in your question. Jan 13, 2017 at 10:12
  • Please check updated answer, above code set 100 value to shipping cost textfield in configuration. Jan 13, 2017 at 10:13
  • please check my updated answer. Jan 13, 2017 at 10:34
  • Thanks, but I still dont understand. I have 3 text columns, can you explain? Why do you set only <interest_rates>100</interest_rates>. I think, do we need to set an array here and how is the format of an array? <interest_rates>array_format_magento2</interest_rates> Jan 13, 2017 at 10:37
0

Sample for new carrier. You can easily set default value for all types of fields

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <carriers>
            <poczta_shipment>
                <active>1</active>
                <model>Your\PathTo\Model\Carrier</model>
                <name>Poczta Polska</name>
                <price>15.00</price>
                <title>Poczta</title>
                <tracking_url>url_for_tracking_code</tracking_url>
            </poczta_shipment>
        </carriers>
    </default>
</config>

EDIT:

Cant You use something like that:

    $this->addColumn('duration', ['label' => __('Duration'), 'style' => 'width:60px', 'default' => 'xxx']);

or

    $this->addColumn('duration', ['label' => __('Duration'), 'style' => 'width:60px', 'value' => 'xxx']);
1
  • Thanks a lots, but I want to set default value for table configuration, I updated my question. Maybe can you take a look again? Jan 13, 2017 at 10:35

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.