Question

Edit: For anyone who may also have this problem, make sure that your spelling is correct, but also that your capitalization is correct. And remember that magento cannot read model files that have any capitalization other than at the start. Aka Testvariable.php works but TestVariable.php will cause it to fail to find.

I have created a custom customer attribute (using generic names for this question) called module in custom extenstion namespace/module. The select attribute is created, but the options for it are blank. If I try 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 to create the select attribute, adapted (hopefully correctly) to be used for a customer attribute instead of a product attribute. The following is all the code for the extension. I have quadruple checked spelling and things and everything is laid out correctly, so maybe I'm missing something needed to make it a customer attribute? I'm on magento EE 1.14.0.1, which is essentially the same as CE 1.9.0.1.

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();
Was it helpful?

Solution 3

In order to be able to mark this question as answered and keep the site's answered percentage up, I am copying my edit answer here:

So after hours more of thinking I had gone crazy, I have discovered my error and feel quite foolish. The variable I was creating I was was calling salesrep, but I capitalized it in the name and inside the model file as SalesRep, which caused magento to fail to find it.

For anyone who may also have this problem, make sure that your spelling is correct, but also that your capitalization is correct. And remember that magento cannot read model files that have any capitalization other than at the start. Aka Testvariable.php works but TestVariable.php will cause it to fail to find.

OTHER TIPS

For those who might have this issue, I solved it by another way. It happened when I disabled a module and some attributes were still trying to be launched in db, even if they were useless.

what i did is this :

delete from eav_attribute where source_model = 'my_source_model'

I have copy paste your code in my magento 1.14.0.1 fresh installation only changing the module and model name and it is working properly. Note that the install script should use the class Mage_Eav_Model_Entity_Setup and that the source module does not own the method $this->__ so that should throw an error as well once magento loads that class.

This is the list of files i have created:

app/local/Test/Test/etc/config.xml

<config>
<modules>
    <Test_Test>
        <version>0.1.0</version>
    </Test_Test>
</modules>
<global>
    <blocks>
        <test>
            <class>Test_Test_Block</class>
        </test>
    </blocks>
    <helpers>
        <test>
            <class>Test_Test_Helper</class>
        </test>
    </helpers>
    <models>
        <test>
            <class>Test_Test_Model</class>
        </test>

    </models>
    <resources>
        <test_setup>
            <setup>
                <module>Test_Test</module>
                <class>Mage_Eav_Model_Entity_Setup</class>
            </setup>
        </test_setup>
    </resources>

</global>

app/local/Test/Test/Model/Attribute/Source/Module.php

    <?php
class Test_Test_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'=> 'Option 1', value=>1);
            $this->_options[] = array('label'=> 'Option 2', value=>3);
            $this->_options[] = array('label'=> '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;
    }
}

app/local/Test/Test/sql/test_setup/mysql4-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"   => "test/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();

app/local/Test/Test/Helper/Data.php

<?php
class Test_Test_Helper_Data extends Mage_Core_Helper_Abstract
{

}

app/etc/modules/Test_Test.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Test_Test>
            <active>true</active>
            <codePool>local</codePool>
        </Test_Test>
    </modules>
</config>

And here is the attribute showing up in the admin panel : enter image description here

Go through this code trying to find what is different in your code, your problem is that the file is not found , magento knows about the attribute when you go to edit a customer, knows the path "namespace/attribute_source_module", however cannot find it, I bet is has something to do with the spelling. if you post your real class names we could all look for spelling mistakes, good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top