5

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

Store view

11

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.

  • when I use this code it shows the options Yes and No in the screen not the stores. Weird stuff, any idea why? – Sander van Zuidam Mar 14 '16 at 15:49
  • maybe you have the wrong source model. – Marius Mar 14 '16 at 15:53
  • It shows the store information and I can select it but how can I actually save the value that was selected? Would be great if I can save it in the core_config_table so I can use it later on. – Sander van Zuidam Mar 16 '16 at 15:30
1

Marius' answer is correct, however, I'd like to add that you can use Mage_Adminhtml_Model_System_Config_Source_Store as your source_model. Which would result in <source_model>adminhtml/system_config_source_store</source_model> in your system.xml field for your config variable.

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.