0

I would like to add some custom content to orders. When I started out, I thought I needed to create two new columns in the quote table and orders table and then add the custom data to the quote table and then add the content to the order table once the order was complete. However, I've subsequently read on a couple of posts that the data in the quote table is automatically moved over to the orders table.

I've tried to find documentation on the process of adding custom data to an quote/order, but I can't find anything. Does anyone have any resource, or could point me in the right direction, for me to learn how to add custom data to an order properly?

1

A couple of salients things to note:

  • If you add custom column(s) to your base Quote and Order tables - you (potentially) risk problems during future upgrades (major/minor versions) of Magento.

  • The background process (well - technically an OOTB M2 observers) that copies data attributes from Quote(s) to the Order(s) objects may not auto-copy custom data columns (i.e. without you overwriting the observer classes to specifically do so)

That being said - here's a 20,000 ft view on how to go about this requirement the Magento "recommended" way:

(1) Create EAV attribute(s) to store the custom data associated with the Order and Quote objects.

This will involve creation of a Setup/UpgradeSchema.php and perhaps even a Setup/UpgradeData.php as well (e.g: in case you need to populate data for priors orders/quotes)

[ I'm assuming you already have a custom-module in place. Hence the upgrade route instead of the install route ]

(2) Setup Observers for the Quote and the Order

The specific event that you will "observe" depends on at what point during the Quote object lifecycle you want/need/wish to source and store your custom attribute(s) For example, if you want to do so during the add-to-cart process perhaps consider observing the checkout_cart_add_product_complete event / or if you want to do so after successful order-capture perhaps consider observing the sales_order_payment_pay / etc.

The same thing applies for the observer you'll create to source/store the custom attribute(s) associated with the Order objects as well - For example, during Order creation / Order Fulfillment / etc.

I hope this helps get the ball rolling! :)

  • At what point is the data in the quote table copied to the sales_order table? Would I be too late to add my content to the quote table if only add it to the quote table when observing sales_order_payment_pay? Is there a list of the different types of 'sales_order_payment_pay' and 'checkout_cart_add_product_complete' I can observe and at what point they're triggered? – MikeMason Nov 15 '17 at 22:56
  • 2
    AFAIK - There isn't an 'official' Magento 2 guide cataloging the different events and when/where/why they're triggered. So, sadly, at this time, the only way (I know of anyways) to figure out which event to "hijack" - for your specific requirement(s) - will require stepping through the code during, the Order capture flow. If you're familiar with XDebug - that should make that identification process easier. P.S: For something I built in the past, I ended up piggybacking off the sales_model_service_quote_submit_before and sales_model_service_quote_submit_success events - but, YMMV. – Sharath Kumar Nov 16 '17 at 5:57
  • 2
    I found this https://gist.github.com/wkw/1e74260b81c3c91f5b21 ... In the interest of full disclosure, I have no idea how accurate and/or up-to-date that info is. But, hopefully, it'll help make the event identification a "little" quicker/easier for you! :) – Sharath Kumar Nov 16 '17 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.