1

I am trying to update the customer created_at (customer since) date due to some weird issues but not able to do so. I have used the following code:

Registering Event:

<frontend>
    ...
    <events>
        <customer_save_before>
            <observers>
                <magepsycho_customer_customer_save_before>
                    <type>singleton</type>
                    <class>magepsycho_customer/observer</class>
                    <method>customerSaveBefore</method>
                </magepsycho_customer_customer_save_before>
            </observers>
        </customer_save_before>            
    </events>
    ...
</frontend>

Observer Model:

public function customerSaveBefore(Varien_Event_Observer $observer)
{
    $event      = $observer->getEvent();
    $customer   = $event->getCustomer();

    if ($customer->isObjectNew()) {            

        //@fix for date issue where day and months are interchanged (esp if registered by bots)
        //get magento UTC date
        $mageDate = $customer->getCreatedAt(); //but this value is always empty
        $mageMon  = date('m', strtotime($customer->getCreatedAt()));
        $mageDay  = date('d', strtotime($customer->getCreatedAt()));

        //get pure UTC date
        $currentMon = date('m');
        $currentDay = date('d');

        if ($currentDay != $mageDay || $currentMon != $mageMon) {
            //set the fixed date
            $fixedDate = now();
            $customer->setCreatedAt($fixedDate);
        }
    }
}

But main issue is $customer->getCreatedAt() is always empty. Not sure if the event is triggered earlier then the date is set.

1
1

I guess you are right about "Not sure if the event is triggered earlier then the date is set." here is code snippet of save() method of class Mage_Core_Model_Abstract. Customer model class extends this class.

$this->_beforeSave();
if ($this->_dataSaveAllowed) {
      $this->_getResource()->save($this);
      $this->_afterSave();
}

Here _beforeSave() method fires event customer_save_before. I think _getResource() method hits db and save customer to db.

So try customer_save_after event which fires at _afterSave() there you may have created_at attribute value.

6
  • If you use customerSaveAfter event it will go infinite loop due to recursive call. – MagePsycho Jul 12 '14 at 10:56
  • Well then If you want to manipulate data at the time of registration, use customer_register_success event, which also fires after save() method, and I guess updated_at attribute will change on every save action not created_at. – Mukund Thanki Jul 12 '14 at 11:03
  • Well the account can be created from checkout page also so this event wont work in that case. customer_register_success event is triggered only if user is registered from customer/account/create link – MagePsycho Jul 12 '14 at 12:10
  • I found this event for checkout, you may have to hook at this event also, checkout_submit_all_after This event will give you $order and $quote you can get customer from them and created_id for customer as well. I don't If this bit late or not for you, at this moment quote is converted to order and new customer email is sent( if registration happens ). – Mukund Thanki Jul 12 '14 at 12:48
  • Main thing is I have to use a singly entry method for modifying the created_at date as customer can register from custom popups, custom landing page, normal registration form, checkout page etc. So I have to find some event similar to customer_save_before. – MagePsycho Jul 12 '14 at 14:11

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.