What I want
- add customer attribute
- show the new attribute in front(dashboard) page
Problem
I have created customer attribute with this code below:
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();
$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType, $vCustAttributeSetId);
$installer->addAttribute('customer', 'manager', array(
'label' => 'Account Manager',
'input' => 'text',
'type' => 'varchar',
'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
'visible' => true,
'global' => true,
'visible_on_front' => 1,
'required' => false,
'default_value' => 'default',
'user_defined' => true,
));
$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId, $vCustAttributeGroupId, 'manager', 0);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'manager');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();
echo "DONE";
exit;
And then, this part was normally works. I can find new form in backend and I can update the form with new data. That was good so far but I couldn't get the data in front page section.
When I have searched this problem, the below code seems to normally working but it wasn't work for me.
I put below code in /app/design/frontend/<Theme>/default/template/customer/account/dashboard.phtml
.
$attr = $this->getCustomer()->getAttribute('manager')->getData($customer);
// not working
$customer = Mage::getSingleton('customer/session')->getCustomer();
$attr = $customer->getAttribute('manager')->getData($customer);
// not working
$_customerModel = Mage::getModel('customer/customer');
$_customer = $customerModel->load($this->getCustomer()->getId());
$attr = $_customer->getAttribute('manager')->getData($_customer);
// Yeah, still not working
$attr = $this->getCustomer()->getAttribute('manager')->getData('manager');
// not working but getData('firstname') is working
I think the custom attribute was not fully loaded when the customer loaded. Also it seems to be problem with new version of magento. I couldn't find same problem anywhere.
Can I get the data by force such as connecting to database directly or am I missing something in this code?
Best Answer
you can get the value of the customer attribute in this way: