0
  1. Login to the website frontend in a customer account.

Click on "Account information" and edit some information (firstname, lastname, ...). Click on the save button. Now login to the magento backend and click on customers -> manage customers. Now you see the grid with the correct "updated_at" timestamp.

  1. Login to the website frontend again. Click on "Account information"

And now click on "Addressbook" and edit some information. Click on the save button. Now go to the magento backend and click on customers -> manage customers. Updated_at is not changed.

Question: How can we set new updated_at timestamp for the address book also?

Address is its own table and has its own updated_at field. If i want to see the timestamp of customer updated when address is updated, i have to an observer for customer_save_after update the timestamp of customer there?

Can you tell me how we can setup an observer for customer_save_after update for the timestamp? In which file i have to add the code?

Thank you and best wishes from germany

3 Answers 3

0

Take care I have not tested that code but the idea is here.

In your config.xml you have to add observer declaration :
<customer_address_save_after> <observers> <update_customer_after_address_save> <class>module/observer</class> <method>updateCustomerAfterAddressSave</method> </update_customer_after_address_save> </observers> </customer_address_save_after>

Then in your Observer.php file add the function:

public function updateCustomerAfterAddressSave(Varien_Event_Observer $observer){
/** @var Mage_Customer_Model_Address $_address */
$_address = $observer->getCustomerAddress();
if (!$_address->hasDataChanges()) {
    return $this;
}

/** @var Mage_Customer_Model_Customer $_customer */
$_customer = $_address->getCustomer();
$_customer->setUpdatedAt(Varien_Date::now())
    ->save();}


Magento have "dynamic" observers launched before/after save/load entities in Mage_Core_Model_Abstract in these functions:
- _beforeSave()
- _afterSave()
- _beforeLoad()
- _afterLoad()

Magento use $_eventPrefix variable to make it dynamic. The Customer_Address one is in Mage_Customer_Model_Address_Abstract. With that you can find the observer hook customer_address_save_after.


Here is an example that explain how to create an extension with an observer : https://magento.stackexchange.com/a/41280/33619
Note that in your case you have to change the observer node from <adminhtml> to <frontend> in the config.xml

7
  • I check the code and give you a feedback in the next 24 hours
    – Tommy
    Jan 4, 2016 at 16:03
  • Can you tell me in which folder i have to create the config.xml file and the Observer.php file?
    – Tommy
    Jan 4, 2016 at 19:18
  • You have to create a custom module, the config.xml is in etc/directory and the Observer.php have to be in the Model/ directory. The creation processus is OK for you or you need an example ? Jan 4, 2016 at 19:29
  • The model/ folder is a subfolder. Where i can find it? The creation processus is not clear for me. I would be very thankfull if you describe the processus
    – Tommy
    Jan 7, 2016 at 12:00
  • Of course no problem : there is a response from Marius that explain how to create a module with Observer. Note that you have to change the node in the config.xml file from adminhtml to frontend. magento.stackexchange.com/a/41280/33619 Jan 7, 2016 at 12:03
0

Filename Observer.php in folder /app/code/local/Easylife/Meta/Model

public function autoMetaDescription(Varien_Event_Observer $observer){
/** @var Mage_Customer_Model_Address $_address */
$_address = $observer->getCustomerAddress();
if (!$_address->hasDataChanges()) {
    return $this;
}

/** @var Mage_Customer_Model_Customer $_customer */
$_customer = $_address->getCustomer();
$_customer->setUpdatedAt(Varien_Date::now())
    ->save();}

Filename config.xml in folder /app/code/local/Easylife/Meta/etc

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <version>0.0.1</version>
        </Easylife_Meta>
    </modules>
    <global>
        <models>
            <easylife_meta>
                <class>Easylife_Meta_Model</class>
            </easylife_meta>
        </models>
    </global>
    <frontend>
        <events>
            <customer_address_save_after><!-- observe the event -->
                <observers>
                    <easylife_meta>
                        <class>easylife_meta/observer</class>
                        <method>autoMetaDescription</method>
                    </easylife_meta>
                </observers>
            </customer_address_save_after>
        </events>
    </frontend>
</config>

Filename Easylife_Meta.xml in folder /app/etc/modules

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <codePool>local</codePool>
            <active>true</active>
        </Easylife_Meta>
    </modules>
</config>

But now i get this error if i edit a adress and click on save in the frontend:

There has been an error processing your request
Cannot send headers; headers already sent in /homepages/16/d280867927/htdocs/dev-brand-top/app/code/local/Easylife/Meta/Model/Observer.php, line 11

Trace:
#0 /homepages/16/d280867927/htdocs/dev-brand-top/lib/Zend/Controller/Response/Abstract.php(148): Zend_Controller_Response_Abstract->canSendHeaders(true)
#1 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Response/Http.php(107): Zend_Controller_Response_Abstract->setRedirect('http://www.dev....', 302)
#2 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Action.php(750): Mage_Core_Controller_Response_Http->setRedirect('http://www.dev....')
#3 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Customer/controllers/AddressController.php(161): Mage_Core_Controller_Varien_Action->_redirectError('http://www.dev....')
#4 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Customer_AddressController->formPostAction()
#5 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('formPost')
#6 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#7 /homepages/16/d280867927/htdocs/dev-brand-top/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#8 /homepages/16/d280867927/htdocs/dev-brand-top/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#9 /homepages/16/d280867927/htdocs/dev-brand-top/index.php(83): Mage::run('', 'store')
#10 {main}
3
  • I edit two things : the name of your mehtod in Observer.php must be the same as the <method> node in config.xml autoMetaDescription. And the <depend> node in Easylife_Meta.xml is useless because you don't have any depends with Mage_Catalog in your extension, if Mage_Catalog is disabled your extension still work. Jan 8, 2016 at 8:53
  • The error you have mean that something is written on the page (response request was sent) before your redirection headers were sent. This kind of errors are hard to find, it is maybe a bad file encoding (BOM at the begining) or maybe a space before the <?php tag. You can add a die before $this->_redirectError function to see if at least there is nothing written on your page before the redirect. If there is none check your file encoding and that there is no space before the <?php tag. Jan 8, 2016 at 8:59
  • I also change your observer node : you observed catalog_product_save_before instead of the customer address one customer_address_save_after Jan 8, 2016 at 9:01
0

Thanks! I make the changes. If i click now the save button in the frontend, the script show effect: Only the normal massage: "adress is saves". But the timestamp in the backend is not changed.

Easylife_Meta.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <codePool>local</codePool>
            <active>true</active>
        </Easylife_Meta>
    </modules>
</config>

Observer.php

<?php
function autoMetaDescription(Varien_Event_Observer $observer)
{
$_address = $observer->getCustomerAddress();
if (!$_address->hasDataChanges())
{
return $this;
}
$_customer = $_address->getCustomer();
$_customer->setUpdatedAt(Varien_Date::now())
->save();
}
?>

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <version>0.0.1</version>
        </Easylife_Meta>
    </modules>
    <global>
        <models>
            <easylife_meta>
                <class>Easylife_Meta_Model</class>
            </easylife_meta>
        </models>
    </global>
    <frontend>
        <events>
            <customer_address_save_after><!-- observe the event -->
                <observers>
                    <easylife_meta>
                        <class>easylife_meta/observer</class>
                        <method>autoMetaDescription</method>
                    </easylife_meta>
                </observers>
            </customer_address_save_after>
        </events>
    </frontend>
</config>

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.