Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 52b6135d authored by Nicolas's avatar Nicolas
Browse files

feat: Create Observer

parent dbbffb82
No related branches found
No related tags found
No related merge requests found
<?php <?php
namespace NicolasBejean\Customer\Observer; namespace NicolasBejean\Customer\Observer;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\Observer; use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Psr\Log\LoggerInterface;
/** /**
* Class SendCustomerDataToElasticsearch * Class SendCustomerDataToElasticsearch
...@@ -15,7 +20,29 @@ use Magento\Framework\Event\ObserverInterface; ...@@ -15,7 +20,29 @@ use Magento\Framework\Event\ObserverInterface;
*/ */
class SendCustomerDataToElasticsearch implements ObserverInterface class SendCustomerDataToElasticsearch implements ObserverInterface
{ {
public function __construct() {} /**
* @var AccountManagementInterface
*/
private AccountManagementInterface $accountManagement;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* SendCustomerDataToElasticsearch constructor.
*
* @param AccountManagementInterface $accountManagement
* @param LoggerInterface $logger
*/
public function __construct(
AccountManagementInterface $accountManagement,
LoggerInterface $logger
) {
$this->accountManagement = $accountManagement;
$this->logger = $logger;
}
/** /**
* Execute method * Execute method
...@@ -27,7 +54,21 @@ class SendCustomerDataToElasticsearch implements ObserverInterface ...@@ -27,7 +54,21 @@ class SendCustomerDataToElasticsearch implements ObserverInterface
$event = $observer->getEvent(); $event = $observer->getEvent();
if ($event->getName() === 'customer_register_success') { 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)) {
/* Send data to Elasticsearch */
}
exit();
} }
} }
} }
<?xml version="1.0"?> <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_register_success"> <event name="customer_register_success">
<observer name="send_customer_data_to_elasticsearch" instance="NicolasBejean\Customer\Observer\SendCustomerDateToElasticsearch" /> <observer name="send_customer_data_to_elasticsearch" instance="NicolasBejean\Customer\Observer\SendCustomerDataToElasticsearch" />
</event> </event>
</config> </config>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment