3

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?

6

you can get the value of the customer attribute in this way:

$customerObj  = Mage::getModel('customer/customer')->load($customer_id);
$attr = $customerObj->getData('manager');
  • Thanks for your answer but that's a same code what I tried. By the way, is it right the code for creating attribute? – Edward Apr 7 '14 at 23:20
  • Sorry. I solved it. You're right. I mean, it was correct. I've got a wrong user information. – Edward Apr 7 '14 at 23:46
  • no problem, I am glad it helped. Cheers! – Raks Apr 8 '14 at 5:46
  • how to set data? – Black Sep 11 '18 at 11:46
2

You should try This link

to create customer attriube.

Just open above link and create module for your customer attribute as Need Add Customer Attribute wity yes it will ask detail for your attribute and it's ready to use with your magento.

Eg. if you are creating your attribute like manager then just add fill the form and create your module.then after you will get your attribute value with cusotomer model like$customer->getManager().

2

Refer the below mentioned coding for Get Logged In Customer’s details

//Check if any customer is logged in or not


if (Mage::getSingleton('customer/session')->isLoggedIn()) {

    // Load the customer's data
    $customer = Mage::getSingleton('customer/session')->getCustomer();

    $customer->getPrefix();
    $customer->getName(); // Full Name
    $customer->getFirstname(); // First Name
    $customer->getMiddlename(); // Middle Name
    $customer->getLastname(); // Last Name
    $customer->getSuffix();

    // All other customer data
    $customer->getWebsiteId(); // ID
    $customer->getEntityId(); // ID
    $customer->getEntityTypeId(); // ID
    $customer->getAttributeSetId(); // ID
    $customer->getEmail();
    $customer->getGroupId(); // ID
    $customer->getStoreId(); // ID
    $customer->getCreatedAt(); // yyyy-mm-ddThh:mm:ss+01:00
    $customer->getUpdatedAt(); // yyyy-mm-dd hh:mm:ss
    $customer->getIsActive(); // 1
    $customer->getDisableAutoGroupChange();
    $customer->getTaxvat();
    $customer->getPasswordHash();
    $customer->getCreatedIn(); // Admin
    $customer->getGender(); // ID
    $customer->getDefaultBilling(); // ID
    $customer->getDefaultShipping(); // ID
    $customer->getDob(); // yyyy-mm-dd hh:mm:ss
    $customer->getTaxClassId(); // ID
}

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.