Newer
Older
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
*
* @category PHP
* @package NicolasBejean\Customer\Observer
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://lab.frogg.it/bejean-developpement/magento-2/modules/customer/-/blob/master/LICENCE GPL3 Licence
* @link https://www.bejean.eu
*/
/**
* @var AccountManagementInterface
*/
private AccountManagementInterface $accountManagement;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var SendGeoLocation
*/
private SendGeoLocation $helper;
/**
* SendCustomerData constructor.
*
* @param AccountManagementInterface $accountManagement
* @param LoggerInterface $logger
*/
public function __construct(
AccountManagementInterface $accountManagement,
) {
$this->accountManagement = $accountManagement;
$this->logger = $logger;
/**
* Execute method
*
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$event = $observer->getEvent();
if ($event->getName() === 'customer_register_success') {
/** @var CustomerInterface $customer */
$customer = $observer->getData('customer');
try {
$defaultBillingAddress = $this->accountManagement->getDefaultBillingAddress($customer->getId());
} catch (NoSuchEntityException | LocalizedException $e) {
$this->logger->error(__('An error has occurred while retrieving the default billing address.'));
exit();
}
if (!is_null($defaultBillingAddress)) {
$this->helper->sendDataToApi($customer, $defaultBillingAddress);