1

I have a custom module that created the table Namespace_Module_Rep_Directory_Rep. In this table we have a set of locations with entity_id, name, email, phone, status.

I want to be able to assign each customer to a rep. I know how to make customer attributes, but I am unsure how to make one that is linked to this table. I want to be able to go to a customer, and for "Rep" select a rep from a dropdown menu populated with the names of the reps from Namespace_Module_Rep_Directory_Rep.

I have looked all over for tutorials or similar setups, but have yet to find anything really useful. If anyone can provide some insight on how to accomplish this or a site that may help me I'd appreciate it, thanks.


Update:

I have followed @Marius's answer and have run into a problem implementing it. When I go to edit a customer, I get the error: "Source model "namespace/attribute_source_module" not found for attribute "module"

It seems to be unable to find the model with the options for the select, so it errors out. I followed the example here, which @Marius provided.

Namespace/Module/etc/config:

...
    <global>
        <models>
            <module>
                <class>Namespace_Module_Model</class>
            </module>
        </models>

...

Namespace/Module/Model/Attribute/Source/Module.php:

<?php
class Namespace_Module_Model_Attribute_Source_Module extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;
    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();

            $this->_options[] = array('label'=> $this->__('Option 1'), value=>1);
            $this->_options[] = array('label'=> $this->__('Option 2'), value=>3);
            $this->_options[] = array('label'=> $this->__('Option 3'), value=>3);
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}

Namespace/Module/sql/module_steup/install-0.1.0.php

<?php
$installer = $this;

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute("customer", "module",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Module",
    "input"    => "select",
    "source"   => "module/attribute_source_module",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Module"

        ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "module");


$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'module',
    '999'  //sort_order
);

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
//$used_in_forms[]="customer_account_edit";
//$used_in_forms[]="adminhtml_checkout";
        $attribute->setData("used_in_forms", $used_in_forms)
                ->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1)
                ->setData("sort_order", 100)
                ;
        $attribute->save();



$installer->endSetup();
3

You just need an attribute with a custom source model.
Here is an example on how to create an attribute with custom options. It's for products but the idea is similar. You said you know how to create attributes. Well...do your magic and just fill in the source setting of the attribute with your model alias.
The model that you use as source must contain a method getAllOptions that should return an array like this:

array(
    array('label' => 'Label 1', 'value' => 'value 1'),
    array('label' => 'Label 2', 'value' => 'value 2'),
    array('label' => 'Label 3', 'value' => 'value 3'),
    ....
)

The label will be show in the dropdown and the value will be saved in the db.

In the example I linked there are 2 method in the source model that you can skip. They are needed only for products and categories. getFlatColums and getFlatUpdateSelect.

  • So it appears in this example the array for the drop down is hard coded in. You hard code the label and the values for the 3 items you want in the drop down. But as I explained in my question, I need to pull those values dynamically from a table in the database. So unless I am misunderstanding the example, I don't believe this will work for me, unfortunately. – The_DemoCorgin Nov 18 '14 at 8:21
  • @tsHunter That's just an example. You can read the values from your table inside that method instead of hard coding the values. I assume you have a model and a collection model associated to that table. You can use Mage::getModel('[module]/[entity]')->getCollection() and loop through the values and build your array. I know my answer is not a complete working solution, but idea is all there. You just need to manipulate the data as you need. – Marius Nov 18 '14 at 8:31
  • Updated my question with code and problem. When implementing your solution, I get errors about failing to find source model whenever I try to edit a customer. Any ideas? Thanks – The_DemoCorgin Nov 19 '14 at 13:29
  • @tsHunter. The code looks ok to me. I don't understand why you get the error. check what get_class(Mage::getModel('module/attribute_source_module')) returns. Maybe you misspelled something or you have a wrong path. – Marius Nov 19 '14 at 13:31
  • get_class(Mage::getModel('module/attribute_source_module')) does not return anything. I have quadruple checked that all spelling and punctuation is consistent across all files. The files match your example. Would I need to extend a different model type because I'm doing customer attributes instead of product attributes? I'm at a loss here for what's going wrong. – The_DemoCorgin Nov 19 '14 at 17:58

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.