I have a code that is being passed in via the URL (akin to the Google source codes) that I want to add to the quote and order. I already have a function that triggers on the event controller_front_init_routers
that adds the data to the session to pull out later, and I've added the field to both the quote and order tables. However, I'm not able to add the data to the quote at that point since the quote doesn't exist in the event. Which event would be the best time to add my code to the quote?
2 Answers
Depending on if you need to take cart contents into consideration my suggestion is to put your custom data into quote using either sales_quote_collect_totals_before
or checkout_quote_init
event.
Then you can observe sales_convert_quote_to_order
event and transfer your data from quote to order.
-
I agree on the collect totals - this is probably how I would handle it. Commented May 24, 2013 at 17:27
-
checkout_quote_init
did it, thanks. No worries about totals, since it doesn't have any real bearing on the cart. It's just traffic tracking info. I already had the convert-to-order taken care of with the sales_convert_quote fieldset.– aynberCommented May 24, 2013 at 17:53
I believe checkout_quote_init
would be suitable for observing whenever a quote is initialized.
Other candidates - inspect the presence of the property on the quote from the session on any of these events - if it does not exist, add it:
Cart add - checkout_cart_product_add_after
Cart update - checkout_cart_update_items_after
Item delete - sales_quote_remove_item
You most likely want to consider other points of entry, though -- for instance when a quote is loaded because a registered customer has logged in - the quote is merged with items they may or may not have added to their guest cart.
Customer login - customer_login
Before merge of guest cart - sales_quote_merge_before
More reading:
You'll want to read this Magento.SE article regarding how to persist this information from the quote to the order:
-
Thanks, checkout_quote_init did it. I already had the code in config.xml to get the quote field to the order.– aynberCommented May 24, 2013 at 17:54