Discussions

Adding info to Order

Just wanted to touch base before I begin. Making data base changes.

I need to add a field that is directly tied to an order... i.e. delivery account for virtual goods. I've already implemented the hooks to add in the pane to the checkout process (need to specify it when entering billing info) and I handle the validation to insure it is a correct account.

I expect I'll need to hook the order view to display this info as well.

No problem... although it took forever to figure out how to do all the hooks and data structures correctly as the documentation is a bit obtuse.

My question, should I just modify the order table to add this addition column? This may be my preference as it is tied directly to an order and must be tracked for that order and will vary from order to order. Alternatively, I can create a new table that links the order to the delivery account. This seems like more work in managing the life cycle of these table entries and insuring they are always deleted if an order is deleted.

My only concern with modifying the order table is that is could cause difficulties when Drupal Commerce updates are installed.

Suggestions?

Okay, looks like the proper way is to actually attach a field to an order. I can do this in the UI via the Order Settings. However I am still stuck on linking the data retrieved from the pane (which isn't a field) to the order field info. I see the field in the order structure but can not find info on how to set that in order to be persistent. Any changes are removed between the submit and review hook functions.

Posted: Dec 13, 2011

Comments

Cogniven on December 15, 2011

I've added a field to the order via the field UI.

Now in the hook_checkout_form_submit I get the data from the pane I added. I can insert this into the $order object passed to this function.

In the hook_commerce_order_presave I see the data I added to the order object. How do I take this data and redirect it to be saved by my field? I see the field_data_field_myorderextra and field_revision_field_myorderextra tables in the database. Do I need to use db_insert/db_update to actually write the records in the table, or is there a field_api function that I am missing.

Unfortunately, the documentation seems a bit obtuse to me in that you create a field which is equivalent to defining a data structure and creating a db table, you create a field instance which is equivalent to creating the specific instance of a this field as in creating a record in the table? Or is this an intermediate step where you are creating a relationship between the object that can contain (I can't use instance because that could be confused with the previous use of that word) data that is in a field db record.

In other words to attempt to equate to an object oriented lifecycle:
Create field = define class
Create field instance = define class relationships (some parent contains field class)
?? = create instances of field which are db records containing field data tied to the parent db record.

What am I missing?

Cogniven on December 16, 2011

This is what I was missing...

The order already had the field entry because I had added it via the field UI.

In the hook_checkout_form_submit all I need to do was set

$order->myfieldname = array('und', => array(0 => array('value' => $valuetoassigntofield)));

where 'und' is the language... Why is the format for field data contained in an entity not defined anywhere?