Automatically assign a customer attribute based on that customer's entity ID during the account creation process?

magento.stackexchange https://magento.stackexchange.com//questions/31571

Question

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.

Was it helpful?

Solution

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

 <global>
       <events>
            <customer_save_after>
                <observers>
                    <your_define_event_name>
                        <class>youtmodulemodelname/observer</class>
                        <method>autosetCustomer</method>
                    </your_define_event_name>
                </observers>
       </events>
       </global>

Observer code:

public function autosetCustomer($observer) {


    $customer = $observer->getCustomer();

     if ($customer->getOrigData('entity_id') || $customer->getCustomId()) {
         return $this;
    }
    $Obcustomer =  $observer->getCustomer();
    $customer->setYourCustomattbute('HH'.$customer->getId());
    // for faster save
     $customer->getResource()->saveAttribute($Obcustomer, 'youattribute_code');
   }

OTHER TIPS

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
    $customer->setCustomId('HH'.$customer->getId());
    $customer->save();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top