My custom module observes the sales_order_place_after
event, and creates a customer and associates the customer with the order by setting customerId
for the order.
What works?
- Order is placed
- Customer is created
- The
customerId
is updated in the order database
What doesn't work?
- The
customerId
is instantly set back toNULL
by another script
How can I find out what script updates the customerId
to NULL
again, after my observer is done running?
1 Answers
Answers 1
You should change event to sales_model_service_quote_submit_success
Example Code (Magento 2 Version): events.xml
<event name="sales_model_service_quote_submit_success"> <observer name="quote_submit_success_observer" instance="Namespace\ModuleName\Observer\GenerateObserver" /> </event>
GenerateObserver.php
... public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Order $order */ $order = $observer->getEvent()->getData('order'); // Ensure customer is registered $this->registerCustomer($order); $order->getResource()->save($order); } protected function registerCustomer( \Magento\Sales\Api\Data\OrderInterface $order ) { /** @var \Magento\Sales\Model\Order $order */ if ($order->getCustomerId() === null) { // Load customer by email, create if not exists. try { $customerData = $this->customerRepository->get($order->getCustomerEmail()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $customerData = $this->orderCustomerManager->create($order->getId()); } $order->setCustomerId($customerData->getId()); } return $this; } ...
0 comments:
Post a Comment