12

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 to NULL by another script

How can I find out what script updates the customerId to NULL again, after my observer is done running?

Joost Meijer
  • 375
  • 2
  • 15
  • hi, have you set customer_is_guest to 0 – faizanbeg Jun 06 '18 at 10:44
  • @faizanbeg I tested my code from a standalone file, and it works. Just when executed from the event, it will be overriding the `customerId` again to `NULL`. My script to set the `customerId` is: `$order->setCustomerId($customer->getId())->setCustomerIsGuest(0)->setCustomerGroupId(1);`. – Joost Meijer Jun 06 '18 at 12:23
  • 1
    You'll get better response at magento.stackexchange.com however, Magento uses a Finite state machine in the ordering processing steps https://en.wikipedia.org/wiki/Finite-state_machine if no other caching/obvious issue. https://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input/9496266#9496266 May help as well. – B00MER Jun 08 '18 at 15:48
  • Setting break points in your IDE to step through the code as it executes could be helpful. – Dut A. Jun 11 '18 at 22:18
  • @DutA. Thats impossible since no PHP editor I know currently is capable of doing that with a framework like Magento. – Joost Meijer Jun 12 '18 at 07:36

2 Answers2

1

I had the same problem - my assumption was that calling the save method on the order triggered whatever was listening to the sales_order_save_before/after events, one of which was setting the customer ID back to null. I worked around this by saving only the attributes I wanted, rather than triggering a save on the entire order:

$order
    ->setCustomerId($customer->getId())
    ->setCustomerIsGuest(0)
    ->setCustomerGroupId($customer->getGroupId());

$order->getResource()
    ->saveAttribute($order, 'customer_id'      )
    ->saveAttribute($order, 'customer_is_guest')
    ->saveAttribute($order, 'customer_group_id');

This allowed me to successfully associate a customer with the order in Magento EE 1.14.3.10 using the sales_model_service_quote_submit_success event.

gregdev
  • 1,863
  • 3
  • 23
  • 28
  • Your answer makes sense, as the `save()` function is the only possible cause that changes back the customer. I'm going to try! – Joost Meijer Sep 24 '18 at 07:36
0

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;
}
 ...
HoangHieu
  • 2,802
  • 3
  • 28
  • 44