Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento associate customer with order after an placed order

Tags:

magento

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?

like image 392
Joost Meijer Avatar asked May 28 '18 12:05

Joost Meijer


1 Answers

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.

like image 72
gregdev Avatar answered Oct 21 '22 17:10

gregdev