0

I need a custom customer attribute of type int that should start with some 5-digit number and auto-increments each time a new customer is created.

Approach 1: Or should I make a simple int type attribute and fill it with customer's auto generated ID plus some 5-digit number as follow.

customer_id = 30;
custom_id = 10030;

Approach 2: Is there a way in magento to create an attribute of input type auto_increment? So I can set the default value to '10000' and it always get increment for new customer? If yes, then how can I make it auto_increment.

$installer->addAttribute('customer', 'cotp_id',  array(
    'type'     => 'int',
    'label'    => 'COTP ID',
    'input'    => 'hidden',
    'source'   => '',
    'visible'  => true,
    'required' => true,
    'default' => '10000',
    'frontend' => '',
    'unique'     => true,
    'note'       => 'Custom Attribute'
));

Approach 3: Or should I try to achieve this through some observer that call upon every time a new customer created, then get the attribute latest/highest value and increment one in it?

1 Answer 1

3

I am suggesting you to used Approach-3 with observer like this.

In your extension config.xml file avilabel here.

app\code\[CodePool]\[Company]\[Extension]\etc\config.xml

define code like this:

<global>
        <events>
            <customer_save_after>
                <observers>
                    <Company_Extension_customer_save_after>
                        <type>singleton</type>
                        <class>Company_Extension_Model_Observer</class>
                        <method>customerRegisterSuccess</method>
                    </Company_Extension_customer_save_after>
                </observers>
            </customer_save_after>
        </events>
    </global>

create Observer.php at

app\code\[CodePool]\Company\Extension\Model\Observer.php

<?php
class Company_Extension_Model_Observer 
{
  public function customerRegisterSuccess(Varien_Event_Observer $observer) 
  {
      //Get Customer detail like 
      $customer = $observer->getEvent()->getCustomer(); 

     // Here you need to add your logic to set that attribute value

  }
}
?>
5
  • Thanks @Dhiren for your answer. But the observer you've mentioned is not observer when customer created from admin. Oct 28, 2016 at 10:38
  • @MuhammadSaiid I edit my answer, if you talking about admin, then you need to used customer_save_after event. Oct 28, 2016 at 10:42
  • If above answer help you, accept as solution, so other get help. Oct 28, 2016 at 10:42
  • Yes thanks @Dhiren I get it. I hope it will be triggered for both admin and frontend customer registration. Oct 28, 2016 at 10:44
  • if you want only for admin, then put event in <adminhtml> instade of <global> Oct 28, 2016 at 10:46

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.