7

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>
    <frontend_type>select</frontend_type>
    <source_model>mymodule_adminhtml/system_config_source_dropdown_shippingmethods</source_model>
    <sort_order>14</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>0</show_in_website>
    <show_in_store>0</show_in_store>
</charge_1_method>
<charge_1_code translate="label">
    <label><![CDATA[Charge <b style="color: #FF7F24;">#1</b> Code]]></label>
    <frontend_type>text</frontend_type>
    <sort_order>15</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>0</show_in_website>
    <show_in_store>0</show_in_store>
</charge_1_code>

... 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?

10

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/

Preview

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.
2

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.