1

I have created an custom select attribute to the customer, Everything is working fine. the attribute save from register and checkout page.

But I wanted to display it in customer dashboard, When I say $customer = Mage::getSingleton('customer/session')->getCustomer() My custom attribute is not in this $customer. Here is my code

upgrade script

$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'foundation', array(
    'type' => 'int',
    'input' => 'select',
    'label' => 'Foundation',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => '0',
    'visible_on_front' => 1,
        'source'=> 'foundation/entity_foundation',
));
if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
      $customer = Mage::getModel('customer/customer');
      $attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
      $setup->addAttributeToSet('customer', $attrSetId, 'General', 'foundation');
}
if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
    Mage::getSingleton('eav/config')
    ->getAttribute('customer', 'foundation')
    ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
    ->save();
}
$installer->endSetup();

Options for select box

<?php
class Npm_Foundation_Model_Entity_Foundation extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions()
    {
        $optionCollections = Mage::getModel('foundation/foundation')->getCollection();
        $options = array();
        foreach ($optionCollections as $foundations) {
            $options[] = array(
                'value' => $foundations->getData('foundation_id'),
                'label' => $foundations->getData('name_fr'));
        }
        return $options;
    }
}

I am trying to get it in customer dashboard as below

$customer = Mage::getSingleton('customer/session')->getCustomer();

Even I tried this also

$customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
 $customer = Mage::getModel("customer/customer")->load($customerId);

added config.xml code

<?xml version="1.0"?>
<config>
    <modules>
        <Npm_Foundation>
            <version>0.1.0</version>
        </Npm_Foundation>
    </modules>
    <admin>
        <routers>
            <foundation>
                <use>admin</use>
                <args>
                    <module>Npm_Foundation</module>
                    <frontName>foundation</frontName>
                </args>
            </foundation>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <foundation>
                    <file>foundation.xml</file>
                </foundation>
            </updates>
        </layout>
    </adminhtml>
    <global>
         <fieldsets>
           <checkout_onepage_quote>
             <customer_foundation>
                 <to_customer>foundation</to_customer>
             </customer_school>
           </checkout_onepage_quote>
            <customer_account>
                <foundation>
                    <to_quote>customer_foundation</to_quote>
                </foundation>
            </customer_account>
        </fieldsets>
        <foundation_setup> 
            <setup>
                <module>Npm_Foundation_Model_Entity_Setup</module> 
                <class>Mage_Customer_Model_Entity_Setup</class>
            </setup>
        </foundation_setup>
        <models>
            <foundation>
                <class>Npm_Foundation_Model</class>
                <resourceModel>foundation_mysql4</resourceModel>
            </foundation>
            <foundation_mysql4>
                <class>Npm_Foundation_Model_Mysql4</class>
                <entities>
                    <foundation>
                        <table>foundation</table>
                    </foundation>
                </entities>
            </foundation_mysql4>
        </models>
        <resources>
            <foundation_setup>
                <setup>
                    <module>Npm_Foundation</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </foundation_setup>
            <foundation_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </foundation_write>
            <foundation_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </foundation_read>
        </resources>
        <blocks>
            <foundation>
                <class>Npm_Foundation_Block</class>
            </foundation>
        </blocks>
        <helpers>
            <foundation>
                <class>Npm_Foundation_Helper</class>
            </foundation>
        </helpers>
    </global>
</config>

But I am not getting the attribute in collection.

Its showing perfectly in admin customer edit section.

Where I am wrong?

7
  • Can you save a value with a customer and check in the customer_entity_int table if the correct value is saved with all the correct relations? (entity ID, attribute ID etc) Sep 11, 2015 at 6:48
  • @SanderMangel, Yes, I am getting the value in backend, When I try to edit from admin. I am getting select box with proper value.
    – Charlie
    Sep 11, 2015 at 6:59
  • ok so in the database it's saved correctly? Sep 11, 2015 at 7:17
  • @SanderMangel, Yes
    – Charlie
    Sep 11, 2015 at 7:18
  • @SanderMangel, you are right, its not saving.. I have just created another customer and checked in back end its showing same for all customer. How can save it ?
    – Charlie
    Sep 11, 2015 at 7:29

1 Answer 1

2

I'm not too sure what's wrong with your script but here's one that works (just used it myself)

$installer = new Mage_Customer_Model_Entity_Setup('core_setup');

$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', 'foundation',  array(
    'input'         => 'select',
    'type'          => 'int',
    'backend'       => '',
    'label'         => 'Foundation',
    'source'        => 'foundation/entity_foundation',
    'visible'       => 0,
    'is_visible'    => 0,
    'required'      => 0,
    'default'       => '0',
    'frontend'      => '',
    'unique'        => 0,
    'user_defined'  => 0,
));

$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'foundation',
    '99'  //sort_order
);

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "foundation");
$attribute->setData('used_in_forms', array('adminhtml_customer'));
$attribute->save();
4
  • I am getting error like Source model "foundation/entity_foundation" not found for attribute "foundation"
    – Charlie
    Sep 11, 2015 at 7:38
  • @Charlie can you also post your config.xml Sep 11, 2015 at 7:44
  • I have added the config.xml code
    – Charlie
    Sep 11, 2015 at 7:53
  • @Charlie can you delete the attribute (from eav_attribute) and try again with source = Npm_Foundation_Model_Entity_Foundation Sep 11, 2015 at 8:54

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.