1

I am using customer_address_save_after event to check do stuff when a customer address has been changed/edited.

I need to somehow prevent the execution of my code if the address hasn't changed. This is because the customer_address_save_after event fires on save customer which might mean the address hasn't been changed but a different customer config setting.

Here is what I have tried so far:

I set the $address variable with getData() and $orig_address with getOrigData()

I then want to check if the data has changed since the last save

if($address != $orig_address && $orig_address['updated_at'] < $address['updated_at']){

      Do stuff...

 }

I have done some searching and seen that there is a _hasDataChanges() method but I've never used it and cannot find any examples of it's use...

Any suggestions are welcomed

UPDATE

I've accessed the _hasDataChanges() method which is always going to be true if the save event fires due to the updated_at customer attribute. I need to find a way to exclude that part of the data from the check...

1 Answer 1

4

Before the after load events are dispatched, _origData and therefore _hasDataChanges are reset.

What you can do: Make your checks in an observer for customer_address_save_before, set a flag in your observer singleton, and check for that flag in customer_address_save_after:

public function checkAddressOnSaveBefore(Varien_Event_Observer $observer)
{
    if ($observer->getModel()->hasDataChanges()) {
        $this->addressHasChanged = true;
    }
}

public function doStuffOnSaveAfter(Varien_Event_Observer $observer)
{
    if ($this->addressHasChanged) {
        do_stuff();
        $this->addressHasChanged = false;
    }
}

The updated_at attribute is automatically set during save, so it should not influence the results in the first observer method.

3
  • Thanks for your feedback, from what I can see the hasDataChanges() method will always be true even if the customer_address_save_before method is called instead of customer_address_save_after This is because the updated_at value is changed as soon as save is dispatched. Sep 20, 2015 at 20:30
  • No, updated_at is set in the attribute backend model, triggered by Mage_Eav_Model_Entity_Abstract::_beforeSave(). This happens after Mage_Core_Model_Abstract::_beforeSave(). Sep 20, 2015 at 20:37
  • Why would hasDataChanges() be set to true if there are no data changes? The only data the has changed is the updated_at or am I missing the point? Sep 20, 2015 at 20:47

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.