1

I am using below event and observer to trigger customer address add and edit.

code for events.xml

 <event name="customer_address_save_after">
    <observer name="customer_addres_after_save_viv_observer" instance="Vendor\Module\Observer\CustomerAddressSaveObserver" />
</event>

Here is the observer file code.

  class CustomerAddressSaveObserver implements ObserverInterface { 

/**
 * @var CustomerRepositoryInterface
 */
protected $customerRepository;
protected $_logger;

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
    \Psr\Log\LoggerInterface $logger
) {
    $this->customerRepository = $customerRepository;
    $this->_logger = $logger;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customerAddress = $observer->getEvent()->getCustomerAddress();
    echo '<pre>';
    print_r($customerAddress->getData());die;
}

}

But the event is not firing for me. Am i using the correct code?

2
  • what you want to do? Oct 2, 2018 at 11:28
  • once the customer changes address or added new address, i need to call one api in that observer Oct 2, 2018 at 11:30

1 Answer 1

3

have a look below code if you still faced issues.

etc/events.xml(make sure your file name)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_address_save_after">
        <observer name="srtpl_customer_address_save_after" instance="Vendor\Frontendsample\Observer\Model" />
    </event>
</config>

Observer/Model.php (make sure you defined the proper path in event.xml at instance tag)

namespace Vendor\Frontendsample\Observer;

class Model implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $customerAddress = $observer->getCustomerAddress();
        //$customer = $customerAddress->getCustomer(); //if you want to get customer details
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($customerAddress->getData());//here you will get address data
    }
}

let me know if you have any query

reference file vendor/magento/module-customer/ObserverAfterAddressSaveObserver.php

don't forget to run below command if your event is not working php bin/magento setup:upgrade && php bin/magento setup:static-content:deploy -f

Code is tested in magento2.2.6

2
  • you are welcome
    – Hitesh
    Oct 2, 2018 at 13:18
  • customer_address_save_after is triggering multiple times in Magento ver. 2.4.2-p2. Any idea on why this is happening? Dec 13, 2021 at 12:05

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.