Add Store Multiselect to Magento System Configuration

magento-1.9multiselect-attributemultistore

I have a scenario where i need to be able to select magento stores like this from the system configuration section.

Store view

Best Answer

This goes in the system.xml under the fields tag.

<field_name_here translate="title" module="[module]">
    <label>Label goes here</label>
    <frontend_type>multiselect</frontend_type>
    <source_model>[module]/store</source_model>
    <sort_order>10</sort_order> <!-- feel free to change this value -->
    <show_in_default>1</show_in_default><!-- can be 0 or 1 -->
    <show_in_website>0</show_in_website><!-- can be 0 or 1 -->
    <show_in_store>0</show_in_store><!-- can be 0 or 1 -->
</field_name_here>

Then create the source model.
For that you need a class inside your module.

app/code/[codebool]/[Namespace]/[Module]/Model/Store.php with this code:

<?php
class [Namespace]_[Module]_Model_Store {
    public function toOptionArray() {
        return Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true);
    }
}

Replace in the code above the values between [] with the appropriate values and you are done.

Related Topic