5

How can I set a default value for a field from system.xml with type time?

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <section id="my_checkout" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Checkout</label>
        <tab>my_tab</tab>
        <resource>My_Checkout::config</resource>
        <group id="cut_off_time" translate="label" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Delivery Cut-off Time</label>
            <field id="cut_off_time_start" type="time" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Start Time</label>
            </field>
        </group>
    </section>
</system>

For field "cut_off_time_start" with type TIME.

2
  • Can you please upload your system.xml? Commented Oct 9, 2018 at 8:45
  • I added example system.xml in question.
    – Jeka
    Commented Oct 9, 2018 at 10:11

2 Answers 2

4

Try this format:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <my_checkout>
            <cut_off_time>
                <cut_off_time_start>02,00,00</cut_off_time_start>
            </cut_off_time>
        </my_checkout>
    </default>
</config>
4
  • @Jeka Does it works for you? Commented Oct 9, 2018 at 10:31
  • I tried separate by "." and ":" but correctly separate by ",".
    – Jeka
    Commented Oct 9, 2018 at 10:34
  • @Jeka Glad to hear it. If my answer has been helpful, please, accept it Commented Oct 9, 2018 at 10:36
  • One thing to be noted is that It did not work when I was using time as the field ID. It worked after I changed it to start_time. Commented Apr 20, 2021 at 10:23
1

Step 1 : create file in this location for creating configuration fields –

app/code///etc/adminhtml/system.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
  <section id="payment">
      <group id="wkmodule" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
          <label><![CDATA[Test Payment Module]]></label>                  
          <field id="active" translate="label" type="select" sortOrder="101" showInDefault="1" showInWebsite="1" showInStore="1">
              <label>Enabled</label>
              <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
          </field>
      </group>
  </section>

Step 2 : Create file to set default values here :

app/code///etc/config.xml

<?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>
        <wkmodule>
            <active>0</active>
            <------>x</---->
        </wkmodule>
    </payment>
</default>

For more reference Click here

2
  • I know how to set the default value for the "select" and the "text", but I do not know how to do it for the "time".
    – Jeka
    Commented Oct 9, 2018 at 10:10
  • How to set multi-select default all value? Commented Jan 28, 2020 at 6:20

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.