Example: Customer is created with entity_id of 115. Customer attribute custom_id is assigned with a prefix of "HH" followed by the entity_id. So the custom_id is "HH115". I assume this would be done with an observer and then happen on the before save of the customer account. Though, i don't know how to get access to the entity_id before the account is actually created.


Object,It is not possible

First of all ,entity_id is auto increment field which value is assigned to Object after any Object Save. Magento did not provide to created this field to before save.

If Magento is provide this feature then this auto increment field value is automatically increase in database whenever data is failed to submit. It will be better idea to use after_save event

Use Magento event customer_save_after for that case


Observer code:

public function autosetCustomer($observer) {

    $customer = $observer->getCustomer();

     if ($customer->getOrigData('entity_id') || $customer->getCustomId()) {
         return $this;
    $Obcustomer =  $observer->getCustomer();
    // for faster save
     $customer->getResource()->saveAttribute($Obcustomer, 'youattribute_code');
  • Why do you have $Obcustomer = $observer->getCustomerAddress(); Shouldn't that be $Obcustomer = $observer->getCustomer(); ? – Marius Aug 7 '14 at 14:10
  • Ah, an after_save would work perfectly. Thank you for your help. – Object Aug 7 '14 at 14:19
  • Give me a sec to try it out! – Object Aug 7 '14 at 14:26

You don't have access to the entity_id before the account is created.
What I don't understand is why you need this. As long as the attribute value is going to be HH{entity_id}, why not just create a method in one of your helpers that will retrieve that value when you need it.
Something like this:

function getSomeCustomerValue($customer) {
    if (!$customer->getId()) {
         return '';
    return 'HH'.$customer->getId();

This way you don't need a new attribute and you don't need to save anything in the db.

But if you insist on having the attribute you need to observe the event customer_save_after to make sure you have the entity_id available.

And the method goes something like this (untested code):

public function afterSaveCustomer($observer) {
    $customer = $observer->getCustomer();
    //if the entity has an original id or if the 'custom_id' is set do nothing
    //this means it's either update or you already set the custom id
    if ($customer->getOrigData('entity_id') || $customer->getCustomId()) {
         return $this;
    //otherwise, generate the custom id and save the customer
  • ,is it good idea to use save() function? – Amit Bera Aug 7 '14 at 14:08
  • 1
    @AmitBera. It's never a good idea to use save :). I added it here of the top of my head. I guess the way you do it with saveAttribute should be faster and safer. – Marius Aug 7 '14 at 14:10
  • The reason this attribute has to exist is because of an arcane connection to an ERP system that requires a unique identifier. The constraints of the system leave me with this odd option. – Object Aug 7 '14 at 14:18
  • then just send to the ERP the customer id. That should be unique. – Marius Aug 7 '14 at 14:20
  • It requires a store based prefix. This is a requirement of their legacy process. I've already suggested that they add the prefix on their end based on the entity_id I send them. It would be less overhead on everyone's part. Believe me, I am not excited about adding a redundant attribute to every customer account. – Object Aug 7 '14 at 14:24

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.