Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Select Store/s Listbox? 
 
derekdon
Jr. Member
 
Total Posts:  21
Joined:  2008-03-18
 

I recently read the PHP Architect’s Guide to Programming with Magento book and created a couple of custom modules with relative ease, but I’m stuck on this so if anyone out there can help that would be great.

As in the Manage Stores area of the admin, my setup looks like this.

Product Website
-Consumer Store
--English
--German
--French
-Business Store
--English
--German
--French

I’ve written a simple custom module to allow the administrator to manage some rate figures that apply to their service/business. They need to have different rate figures for both the consumer and business stores, so when entering a new rate I’d like to have a 2 row high multiple select list that allows them to specify which store/s the rate is for.

In my Project_Rate_Block_Adminhtml_Rate_Edit_Tab_General class I’m trying to do something like this…

...

$fieldset->addField('store_ids''select', array(
          
'label'     => Mage::helper('rate')->__('Select Stores'),
          
'name'      => 'store_ids',
          
'values'   => Mage::getModel('core/store')->getCollection()->toOptionHash()
         ));

...

$form->getElement('store_ids')->setSize(2);

So far all I get is a standard drop down list with the labels:

English
German
French
English
German
French

Source is…

<select id="store_ids" name="store_ids" type="select" class="select">
<
option value="1">English</option>
<
option value="5">German</option>
<
option value="6">French</option>
<
option value="7">English</option>
<
option value="8">German</option>
<
option value="9">French</option>
</
select>

Any idea how I can do this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
derekdon
Jr. Member
 
Total Posts:  21
Joined:  2008-03-18
 

Anyone know how to do this? I imagine it’s easy when you know how…

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

$options Mage::getModel('core/store_group')->getCollection()->toOptionArray();
foreach ( 
$options as &$option {
    $group_id 
$option["value"];
    
$option["value"Mage::getModel('core/store')->getCollection()->addGroupFilter($group_id)->toOptionArray();
}
$fieldset
->addField('store_ids''multiselect', array(
    
'label'  => Mage::helper('rate')->__('Select Stores'),
    
'name'   => 'store_ids',
    
'values' => $options
));

EDIT:  I missed the ‘multiselect’ part of the problem.  Updated to work..

 
Magento Community Magento Community
Magento Community
Magento Community
 
derekdon
Jr. Member
 
Total Posts:  21
Joined:  2008-03-18
 

Lee you’re a legend man! Thanks a million, that’s exactly what I was after! Hopefully I’ll get to grips with the inner workings of Zend and Magento soon. The documentation could do with a few examples of common tasks and a couple more method descriptions. I’m pretty sure I know what’s going on with your example, although I’m not 100% on one or two things.  Just to clarify, would this be right…

Create an array of the store groups, i.e. Consumer, Business. Loop through this array and override the value of group.value (which originally is just the group ID?) with an array of the stores that are in this group, that’s what the addGroupFilter method does right? Then make sure to set the field to multiselect and set the values to the options array. This right?

Have you any idea where is the method addGroupFilter is so I can have a look at the code?

Thanks again! grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

@derekdon

Sorry, had a little problem posting before, but I’m back.

You are correct, it creates a list of Store Groups then iterates over them and creates a filtered list of stores for each group.  It replaces the value key for each group with an array.  This format allows the select/multiselect element renderer to recognized the top level as <optgroup> elements for you.

And yes, addGroupFilter() does the filtering for you.  You can find it at /magento/app/code/core/Mage/Core/Model/Mysql4/Store/Collection.php

EDIT: Just fixed some typos and added some formatting

 
Magento Community Magento Community
Magento Community
Magento Community
 
derekdon
Jr. Member
 
Total Posts:  21
Joined:  2008-03-18
 

Thanks a million Lee, it’s great to get a solution on these forums for once… Very helpful! I’ll check out that Class later, Cheers. Derek.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top