简体   繁体   中英

Magento address save after hook & database update

I have question about magento hook "customer_address_save_after" & "address_save_after" . I'd to make request from this hook to API, which gets User from Magento database. Unfortunnaly at this moment, in database are old data - not updated yet.

Is there any hook in Magento, which allow me make request AFTER database update? Or maybe is any method to save data in this hook to database? Something like $customer->save() ?

public function afterAddressSave($observer)
{
    $customerAddress = $observer->getCustomerAddress();
    $customer = $customerAddress->getCustomer();

    if ($customer->getDefaultBillingAddress() && $customer->getDefaultBillingAddress()->getCountryId()) {
        Mage::helper('iwsettings')->setStoreId($customer, $customer->getDefaultBillingAddress()->getCountryId());
    }
    $this->updateTypo3UserData($customer);
}

I believe you are confused a bit, the customer_address_save_after event is fired after data is committed to the database, however that data will not "stick" in the database if you throw an exception or a die() within your observer, as this causes the database to "roll back the save"

customer_address_save_after

  • $observer->getCustomerAddress() This is the address being submitted
  • Mage::getModel('customer/address')->load($id) Within the observer, by passing the id of the $observer->getCustomerAddress()->getEntityId() we can see the exact same data as above

If however, you use customer_address_save_before

  • $observer->getCustomerAddress() This is the address being submitted
  • $observer->getCustomerAddress()->getEntityId() will be blank if new, and will have the address id if edited

This does leave you with the issue of if the address is new, you don't know the entity ID yet (not until after save). However as the Observer is loaded as a singleton you can set a flag on your observer, and fetch the address id later on after save (it'll be there). As long as your observer does not throw an exception or die it'll save to the database!

So, for your query, you can use customer_address_save_after (there is also a customer_save_after if you want the customers values). just make sure nothing throws exception, ie wrap your code in a try{}catch{} and fail it silently if it pulls an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM