Custom parameters via de admin->configuration instellen

In modules wil je regelmatig dat gebruikers zelf een aantal parameters kunnen instellen via het configuratie scherm van Magento in de backend. Daarvoor moet je het volgende doen.

Definiëer de opties in de config.xml van je module.

YourModule/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_YourModule>
            <version>0.0.1</version>
        </YourCompany_YourModule>
    </modules>
    <global>
        <models>
            <yourmodule>
                <class>YourCompany_YourModule_Model</class>
            </yourmodule>
        </models>
    </global>
    <default>
        <yourmodule>
            <yourmodel>
              <option_one></option_one>
                <option_two>0</option_two>
            </yourmodel>
        </yourmodule>
    </default>
</config>

Definiëer de volgorde en de waardes van de opties in de system.xml van je module.

YourModule/etc/system.xml:

<!--?xml version="1.0"?-->
<config>
   <sections>
      <yourmodule translate="label" module="yourmodule">
            <label>Your Module</label>
            <tab>sales</tab> # These settings will be visible in the Tab: Sales
            <frontend_type>text</frontend_type>
            <sort_order>1000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                    <label>Your Module Settings</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                      <option_one translate="label">
                            <label>Option One</label>
                            <frontend_type>multiselect</frontend_type>
                            <source_model>ordermonitor</source_model>/system_config_source_payment_activemethods # This example shows all active payment methods
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                      </option_one>
                      <option_two translate="label">
                            <label>Option Two</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno # Shows selection box Yes or No
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                      </option_two>
                </fields>
            </groups>
      </yourmodule>
   </sections>
</config>

Lees de opties uit in je Model van de module.

YourModule/Model/YourModel.php:

<?php

class YourCompany_YourModule_Model_YourModel {
  const XML_PATH_OPTION_ONE = 'yourmodule/yourmodel/option_one';
  const XML_PATH_OPTION_TWO = 'yourmodule/yourmodel/option_two';

  public function run() {
    $option_one = Mage::getStoreConfig(self::XML_PATH_OPTION_ONE);
    $option_two = Mage::getStoreConfig(self::XML_PATH_OPTION_TWO);

    echo $option_one;
    echo $option_two;
  }

}

Deel dit met anderen

Ed de Tollenaer

Geef uw reactie