diff --git a/Observer/SendCustomerData.php b/Observer/SendCustomerData.php deleted file mode 100644 index 727ed4aaf93f6234b31331c347aebb1a329de1e4..0000000000000000000000000000000000000000 --- a/Observer/SendCustomerData.php +++ /dev/null @@ -1,83 +0,0 @@ -<?php -namespace NicolasBejean\Customer\Observer; - -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; -use NicolasBejean\Customer\Helper\SendGeoLocation; -use Psr\Log\LoggerInterface; - -/** - * Class SendCustomerData - * - * @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 - */ -class SendCustomerData implements ObserverInterface -{ - /** - * @var AccountManagementInterface - */ - private AccountManagementInterface $accountManagement; - - /** - * @var LoggerInterface - */ - private LoggerInterface $logger; - - /** - * @var SendGeoLocation - */ - private SendGeoLocation $helper; - - /** - * SendCustomerData constructor. - * - * @param AccountManagementInterface $accountManagement - * @param LoggerInterface $logger - * @param SendGeoLocation $helper - */ - public function __construct( - AccountManagementInterface $accountManagement, - LoggerInterface $logger, - SendGeoLocation $helper - ) { - $this->accountManagement = $accountManagement; - $this->logger = $logger; - $this->helper = $helper; - } - - /** - * 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); - } - - exit(); - } - } -} diff --git a/Plugin/ModelAddress.php b/Plugin/ModelAddress.php new file mode 100644 index 0000000000000000000000000000000000000000..eb62ff17877b51be2988a6775b3ced516e0fabf9 --- /dev/null +++ b/Plugin/ModelAddress.php @@ -0,0 +1,42 @@ +<?php +namespace NicolasBejean\Customer\Plugin; + +use NicolasBejean\Customer\Helper\SendGeoLocation; + +/** + * Class ModelAddress + * + * @category PHP + * @package NicolasBejean\Customer\Plugin + * @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 + */ +class ModelAddress +{ + /** + * @var SendGeoLocation + */ + private SendGeoLocation $helper; + + /** + * ModelAddress constructor. + * + * @param SendGeoLocation $helper + */ + public function __construct( + SendGeoLocation $helper + ) { + $this->helper = $helper; + } + + /** + * @param \Magento\Customer\Model\Address $subject + */ + public function afterSave(\Magento\Customer\Model\Address $subject) + { + $customer = $subject->getCustomer(); + + $this->helper->sendDataToApi($customer, $subject); + } +} diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000000000000000000000000000000000000..79b2d3aef31b1aa84d2e9ee8da7ffea6d42bbbf8 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,6 @@ +<?xml version="1.0"?> +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> + <type name="Magento\Customer\Model\Address"> + <plugin name="NicolasBejeanModelAddress" type="NicolasBejean\Customer\Plugin\ModelAddress" sortOrder="1" disabled="false" /> + </type> +</config> diff --git a/etc/events.xml b/etc/events.xml deleted file mode 100644 index 9f4ff2e27b2c1da49b1fb5ec9192c527772b8822..0000000000000000000000000000000000000000 --- a/etc/events.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?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_register_success"> - <observer name="send_customer_data" instance="NicolasBejean\Customer\Observer\SendCustomerData" /> - </event> -</config>