Using this guide I added a custom line item type to my Drupal Commerce installation and have allowed the customer to add extra data when purchasing an item. When the order is complete I need to get hold of that extra data and use it to build some database rows that will grant them access to the service they have just purchased. I have implemented hook_commerce_checkout_complete($order) but I haven't been able to find the data within $order. I know it is there somewhere because I can view the order on the customer's account and have modified the View to show the data next to Unit price, Quantity and Total columns.
1
You can use the entity_metadata_wrapper
on commerce_order
/**
* Implements hook_commerce_checkout_complete
*/
function package_designer_commerce_checkout_complete($order) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if(isset($line_item_wrapper->field_package_configuration)){
$package_configuration = $line_item_wrapper->field_package_configuration->value();
// You can also get the raw value like this:
// $package_configuration = $line_item_wrapper->field_package_configuration->raw();
}
}
}
0
This is the solution I have so far, but I'm new to Drupal and expect to be corrected. The extra field I added to my custom line item is called *package_configuration*
function package_designer_commerce_checkout_complete($order) {
$ow = entity_metadata_wrapper('commerce_order', $order);
foreach ($ow->commerce_line_items as $line_item) {
$lineItemId = $line_item->line_item_id->value();
$result = db_query("select * from field_data_field_package_configuration where entity_id = $lineItemId");
foreach($result as $record){
$customValue = $record->field_package_configuration_value;
}
}
}
-
1You can probably just use
$line_item->field_package_configuration->value()
without thedb_query
– David Thomas Oct 15 '13 at 2:52