0

When creating a new customer in admin, you are presented with a select box to select a customer group. E.g.

  • A custom group
  • Another custom group

I have extended the table customer_group to include a column for external_id which is imported from another source. I would like to include the value for external_id in this select box. E.g.

  • A custom group (82726)
  • Another custom group (23234)
  • etc.

Does anyone know how this can be accomplished?

Many thanks!

1 Answer 1

2

Checking the eav_attribute table for the attribute group_id you can see the source model (The class that returns the options for the dropdown) is customer/customer_attribute_source_group.

The getAllOptions method uses the class Mage_Customer_Model_Resource_Group_Collection to actually get the array, so we'll rewrite that class.

In it's config.xml you'll need the following

[...]
<global>
    [...]
    <models>
        <customer_resource>
            <rewrite>
                <group_collection>[Namespace]_[Module]_Model_Customer_Resource_Group_Collection</group_collection>
            </rewrite>
        </customer_resource>
    </models>
    [...]
<global>
[...]

Now in the Model directory create a file with path Customer/Resource/Group/Collection.php with the following

class [Namespace]_[Module]_Model_Customer_Resource_Group_Collection extends Mage_Customer_Model_Resource_Group_Collection
{
    /**
     * Convert items array to array for select options
     *
     * rewritten from Varien_Data_Collection
     *
     * @param   string $valueField
     * @param   string $labelField
     * @return  array
     */
    public function toOptionArray($valueField='id', $labelField='name', $additional=array())
    {
        $res = array();

        foreach ($this as $item) {
            $res[] = array(
                'value' => $item->getData($valueField),
                'label' => "{$item->getData($labelField)} ({$item->getData('external_id')})",
            );
        }

        return $res;
    }
}

Which should return you a dropdown with the correct labels

5
  • This works, although in 1.8.1.0 the function is public function toOptionArray($valueField='id', $labelField='name', $additional=array())
    – Fredrik
    Commented Sep 10, 2015 at 10:58
  • So it's not calling _toOptionArray in class Varien_Data_Collection? Thanks! didn't know that Commented Sep 10, 2015 at 11:00
  • Maybe I was a bit quick with the comment. In Mage_Customer_Model_Resource_Group_Collection::toOptionArray it calls parent::_toOptionArray('customer_group_id', 'customer_group_code'); which (if I'm understanding correctly) can't be overwritten at this level. Either way I had to overwrite public function toOptionArray for it to work.
    – Fredrik
    Commented Sep 10, 2015 at 11:05
  • Actually I think protected methods can be overwritten. But didn't test it. I'll change my answer to the public one! Commented Sep 10, 2015 at 11:23
  • Yes, protected method can be overwritten, but since it's calling parent:: _toOptionArray I dont think you can overwrite this call.
    – Fredrik
    Commented Sep 10, 2015 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.