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 55087bfe authored by Nicolas's avatar Nicolas
Browse files

feat: Delete Observer | Create Plugin

parent 3827ed6a
No related branches found
No related tags found
No related merge requests found
<?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();
}
}
}
<?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);
}
}
<?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>
<?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>
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