8

I need to create a custom module which allows admin to create Zones for different countries. I need to add 'allow country' field (just like in default magento System > Configuration > General > Countries Options) in my form where admin can select the countries for a zone.

I Just need to add a countries multi select field in my custom module admin form.

Can anyone help me how to do this. Thanks..

  • sorry forgot to ask, you wanna add this in your custom config section or admin form? – Shathish Oct 28 '13 at 5:47
  • I need to add the field in my custom module admin form. – Jaimin Sutariya Oct 28 '13 at 5:50
6

I found the solution.
To add a country multi select dropdown in your form you need to add below code in your Block/Adminhtml/ModuleName/Edit/Tab/Form.php file.

$countryList = Mage::getModel('directory/country')->getResourceCollection()->loadByStore()->toOptionArray(true);
$fieldset->addField('countries', 'multiselect', array(
            'name'      => 'countries[]',
            'label'     => Mage::helper('zones')->__('Countries'),
            'title'     => Mage::helper('zones')->__('Countries'),
            'required'  => true,
            'values'    => $countryList,
        ));
| improve this answer | |
6

To bring in the allowed countries field in your custom module config:

add the following to your module's system.xml

<sallowspecific translate="label">
    <label>Ship to Applicable Countries</label>
    <frontend_type>select</frontend_type>
    <sort_order>90</sort_order>
    <frontend_class>shipping-applicable-country</frontend_class>
    <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>0</show_in_store>
</sallowspecific>
<specificcountry translate="label">
    <label>Ship to Specific Countries</label>
    <frontend_type>multiselect</frontend_type>
    <sort_order>91</sort_order>
    <source_model>adminhtml/system_config_source_country</source_model>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>0</show_in_store>
    <can_be_empty>1</can_be_empty>
</specificcountry>

under <fields> tag in your custom section.

To add it in the admin form:

in app/code/local/Yourmodulename/Block/Adminhtml/Yourmodulename/Edit/Tab/Form.php

$countryList = Mage::getModel('directory/country')->getResourceCollection()->loadByStore()->toOptionArray(true);
$fieldset->addField('allowed_countries', 'multiselect', array( /* "allowed_countries" is the column name in your custom table to store these values */
    'name'      => 'countries[]',
    'label'     => Mage::helper('yourmodulename')->__('Allowed Countries'),
    'title'     => Mage::helper('yourmodulename')->__('Allowed Countries'),
    'required'  => true, /* only if it is required */
    'values'    => $countryList,
));

note:

  • You have to write logic to save the multiselect values in database in saveAction()

to display this in admin grid:

refer this link.

| improve this answer | |
  • To save the multiselect values you just need to implode the post data using "," (eg. $countries = implode(",",$selectedCountries)) and save it in database. Magento will do everything else to get it back on edit or view page. – Jaimin Sutariya Oct 28 '13 at 9:38
3
$fieldset->addField('country', 'select', array(  
        'name' => 'country',  
        'label' => 'Country',  
        'values' => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),  
        ));
| improve this answer | |

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.