I have a custom module with adminhtml setting (system.xml), which allows the the user to assign a unique "code" to a specific shipping method.

It looks something like this:

enter image description here

As you can see, this is pretty basic as the requirement was only for 5 mapping. This was done like this using the system.xml like this:

<charge_1_method translate="label">
    <label><![CDATA[Charge <b style="color: #FF7F24;">#1</b> Method]]></label>
<charge_1_code translate="label">
    <label><![CDATA[Charge <b style="color: #FF7F24;">#1</b> Code]]></label>

... etc

Now the requirement has changed, where I have to map around 25 different shipping methods to a unique code. So, I'd like to have this portion of the setting functioning dynamically.

So, user can create as many key ==> value pair matching as they want.

Key has to be the "shipping method" from my custom dropdown select source. Value has to be an input field.

Any idea on how I can achieve this? Are there any examples of this?


So, user can create as many key ==> value pair matching as they want.

For this I would recommend the "Array" frontend and backend models, which work similar to the tier price definition in products. You would create a matrix with 2 columns that can easily be transformed to a key=>value map if needed. With a little extension to the frontend model you can use dropdowns for the shipping method as well.

It is explained in detail here: http://www.integer-net.com/2015/03/17/how-to-create-tables-in-magento-system-configuration/


example from integer-net.com

What you need

  • Array backend model, will save the matrix as serialized array: <backend_model>adminhtml/system_config_backend_serialized_array</backend_model>
  • Custom frontend model that extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract and overrides _prepareToRender() to define the columns.
  • A renderer that extends Mage_Core_Block_Html_Select to create a custom dropdown.

Here is a tutorial on how to add create a system->config section with dynamic number of fields.
The example shows how to create on field for each customer group, but you can change that to fit your needs.
The main idea is to create a custom frontend_model for the config group. It is called frontend_model but it's actually a block.
That block must have a method called _getFieldHtml that you can use to add how many fields you need to a fieldset and then return the html of that fieldset.

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.