1

I'm attempting to implement encryption on certain customer data in a fresh install of Magento 1.9.3.6. I'm aware that this will cause numerous issues in the Magento dashboard, and probably elsewhere, but I'm just trying to put together a proof-of-concept.

I've succeeded in encrypting the data using the customer_address_save_before event, I'm looking for a way to reliably decrypt the contents of the address each time it is loaded.

For now I'm working inside of the core Customer module until I get it working, at which point I'll move the code out into a module of it's own.

I've created a new function in the

 app/code/core/Mage/Customer/Model/Observer.php 

file, called afterAddressLoad, which I have successfully managed to run by making use of another event. I've included the <events> section of the config.xml file below, note the top two events are unchanged from the stock file:

<events>
    <customer_address_save_before>
        <observers>
            <customer_address_before_save_viv_observer>
                <class>customer/observer</class>
                <method>beforeAddressSave</method>
            </customer_address_before_save_viv_observer>
        </observers>
    </customer_address_save_before>
    <customer_address_save_after>
        <observers>
            <customer_addres_after_save_viv_observer>
                <class>customer/observer</class>
                <method>afterAddressSave</method>
            </customer_addres_after_save_viv_observer>
        </observers>
    </customer_address_save_after>
    <customer_address_load_after>
        <observers>
            <customer_address_after_load_viv_observer>
                <class>customer/observer</class>
                <method>afterAddressLoad</method>
            </customer_address_after_load_viv_observer>
        </observers>
    </customer_address_load_after>
</events>

The customer_address_save_before and customer_address_save_after events function fine, but the customer_address_load_after does not fire at all when loading a customer address.

Changing the event to customer_load_after at least runs the function when the customer data is accessed, but then the contents of the $observer variable is the Customer object, not the Address object which I need to modify.

How can I do this? If the customer_address_load_after event won't work, is there a way I can alter the contents of the loaded address by making use of some other event which fires each time the address is loaded?

Any help would be much appreciated!

EDIT: The relevant function in the Observer.php file is below:

class Mage_Customer_Model_Observer
{
    public function afterAddressLoad($observer)
    {
        Mage::log($observer);
    }
}

This function runs fine when bound to other events, just not the customer_address_load_after event. Caching is disabled on my install.

  • Please put your observer too. Also, make sure you are removed Magento cache after you update the config.xml file – Rajeev K Tomy Oct 3 '17 at 4:16
0

Event "customer_address_after_load" not exist in magento that the reason you can't hook this event . Either create "customer_address_after_load" event or used all ready created event in magento

For creating a any custom follow some important step as:

step 1. A controller dispatches the event customer_address and broadcasts the customer data.

Mage::dispatchEvent('customer_address', array('customer' => $customer));

step 2.The event configuration in the module’s config.xml file. It says run the function processCustomerData in the php class PackageName_ModuleName_Model_Observer when the event customer_address is happened.

<frontend>
<events>
    <enroll_success>
        <observers>
            <customer_address_observer>
                <type>singleton</type>
                <class>PackageName_ModuleName_Model_Observer</class>
                <method>processCustomerData</method>
            </customer_address_observer>
        </observers>
    </enroll_success>
</events>
</frontend>

step 3. The function processCustomerData that reacts to the event ‘customer_address‘

public function processCustomerData(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$customer = $event->getCustomer();
//Do something with the customer data
}
  • Thanks, this is very helpful, it seems that few of the events I've used with success are documented as being part of Magento either. Certainly odd that the address would have a save_before, save_after, and load_before event, but not a load_after one! Do you know where I could put the dispatchEvent you mentioned in step 1, such that it runs when the customer address is accessed? Ideally I'd avoid putting it in existing core code, is this possible? – Dan Oct 3 '17 at 14:53
0

I've managed to solve this issue. I'll share the solution here for anyone who might come across this in the future. Mudit's answer was helpful, but after searching for the dispatchEvent call associated with many events I know work, and finding nothing, I realised that perhaps the issue was not that the event wasn't being dispatched, rather that the address isn't always accessed through the address class.

It seems customer_address_load_after is indeed an existing event, created in the Mage_Core_Model_Abstract file's _afterLoad() method. The "customer_address" half is passed in as a variable and is concatenated with "_load_after" to produce the event name, hence why I couldn't find the event name in any files. It seems that many uses of the address (including in the dashboard where I was testing) get the address from an instance of the customer class, rather than using the address class. The event does not fire when the address is accessed this way. When a user is logged in and looking at their address, the billing and shipping addresses are accessed through the Address class, and the event fires as expected (albeit four times). Confusingly, additional addresses on the same page are accessed via the customer class, meaning they did not trigger the event.

I solved this problem by also applying my decryption code to each of the arrays returned by $observer->getCustomer()->getAddresses(); in my afterCustomerLoad observer function, ensuring it runs however the address is accessed. Now the addresses are altered both in the dashboard and from the customer account settings.

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.