Home » Magento Tutorial » Magento Tutorials » Lesson 22: Magento Custom Entity/Address (part 1)
Lesson 22: Magento Custom Entity/Address (part 1)

Lesson 22: Magento Custom Entity/Address (part 1)

As you know, Magento Community only supports product attribute management function and does not have customer attribute management function which is available in Magento Enterprise. So, in today lesson we will learn customer attribute/address.

You should use three hours to cover all the following sections:

  • How to add attribute to Customer
  • How to show attribute in Edit Customer form in backend
  • Types of customer address (Address type – address book)
  • How to add a new tab to Edit Customer form in Backend

However, this lesson is divided into two parts and posted weekly to make it easier for you to follow and practice.

1. How to add attribute to Customer and show in Edit Customer form

For example: Attribute National ID is shown in Edit Customer form

customer attribute

  1. Create module Magestore_Lesson22 with module creator
  2. Add the following code to installation file

app/code/local/Magestore/Lesson22/sql/leson22_setup/mysql4-install-0.1.0.php

$setup = new Mage_Eav_Model_Entity_Setup(‘customer_setup’);
$setup->addAttribute(‘customer’, ‘national_id’, array(
‘type’ => ‘varchar’,
‘input’ => ‘text’,
‘label’ => ‘National ID’,
‘global’ => 1,
‘visible’ => 1,
‘required’ => 0,
‘user_defined’ => 1,
‘visible_on_front’ => 1
));
Mage::getSingleton(‘eav/config’)
->getAttribute(‘customer’, ‘national_id’)
->setData(‘used_in_forms’, array(‘customer_account_create’,’customer_account_edit’,’adminhtml_customer’))
->save();
$setup->addAttribute(‘customer’, ‘store_credit’, array(
‘type’ => ‘int’,
‘input’ => ‘text’,
‘label’ => ‘Store Credit’,
‘global’ => 1,
‘visible’ => 1,
‘required’ => 0,
‘user_defined’ => 1,
‘visible_on_front’ => 1
));
$installer->endSetup();
  • National_id is attribute code
  • National ID: attribute label (name) is shown in frontend/backend
  • Type: It specifies attribute data type. Magento has six data types: static, datetime, int, text and varchar. The static type is special as the attributes with static type are stored in main entity table.
  • Input: Magento supports the following input:Multiselect: Multiple Select
    • Text: Text Field
    • Textarea: Text Area
    • Date: Date
    • Boolean: Yes/No
    • Multiselect: Multiple Select
    • Select: Price
    • Media_image: Media Image
    • Weee: Fixed Product Tax
  • Global: Usage limit of attribute (global/store view)
  • User_defined: whether user-generated attribute or Magento system attributes.

Note: Statement:

[php] $installer = $this;
$installer->startSetup();
Mage::getSingleton(‘eav/config’)
->getAttribute(‘customer’, ‘national_id’)
->setData(‘used_in_forms’, array(‘customer_account_create’,’customer_account_edit’,’adminhtml_customer’))
->save();[/php]

Parameter used_in_forms identifies in which forms attribute is used:

  • adminhtml_customer: attribute is shows forms in backend
  • customer_account_create: attribute is shows in form which creates account
  • customer_account_edit: attribute is shown in form which edits account

For forms in frontend, we need to edit form’s template form (create account, edit account and checkout form) to show custom attributes.

When module setup finishes, in create/edit customer form, attribute National ID is shown as the above screenshot.

I create attribute store_credit in this setup file and do not show it in Edit Customer form of Magento.  I will show this attribute in Custom Tab which is added to Edit Form (see sections 3 in part 2 for more detail).

2. Customer Address

In Magento, Customer Address is built in EAV model. There are two kinds of address: billing address and shipping address. A customer can have many different billing addresses and shipping addresses.

To add attribute to address, add the following code to installation file of module:

app/code/local/Magestore/Lesson22/sql/leson22_setup/mysql4-install-0.1.0.php

[php]$setup = new Mage_Eav_Model_Entity_Setup(‘customer_setup’);
$setup->addAttribute(‘customer_address’, ‘mobile_number’, array(
‘type’ => ‘varchar’,
‘input’ => ‘text’,
‘label’ => ‘Mobile’,
‘global’ => 1,
‘visible’ => 1,
‘required’ => 0,
‘user_defined’ => 1,
‘visible_on_front’ => 1
));
Mage::getSingleton(‘eav/config’)
->getAttribute(‘customer_address’, ‘mobile_number’)
->setData(‘used_in_forms’, array(‘customer_register_address’,’customer_address_edit’,’adminhtml_customer_address’))
->save();[/php]

I create mobile_number attribute to save customers’ mobile phone numbers. The meaning of parameters is similar to that in creating attribute for customer function.

After module is set up, attribute is shown in Edit Customer’s Address in backend.

add customer attribute

So, our lesson today finishes here, we hope that it’s useful for you. Next week, we will go together the other of lesson 22: How to add a new tab to Edit Customer form in Backend.

 

About Alex

Alex is Magestore’s Project Manager. His character is open-hearted and smart . He possesses a thinking-ability making you surprised much. Don’t hesitate to contact with him for wise advices of technique.

One comment

  1. It’s an informative blog, provide some basic step information about custom entity and custom address information which could be helpful for securing billing and provide information about user.