2

When the checkout process is complete I'd like to record the SKU and the user ID in a separate table in my database.

Is the best approach to use the Rules module, using the 'Completing the checkout process' event and writing a custom action?

If so, how would I be best going about this?

Advice on how best to achieve the above would be appreciated.

4

I don't know of any modules that enable you to write db records in rules actions.

I guess you could write a custom rules action to write to your custom table and grab all the data in rules but it's just as easy to do something like below using hook_commerce_checkout_complete:

The trickiest part is obtaining the SKU

<?php
  function hook_commerce_checkout_complete($order) {
    $ow = entity_metadata_wrapper('commerce_order', $order);

    foreach ($ow->commerce_line_items as $line_item) {
     $sku = $line_item->commerce_product->sku->value();

     $record = array(
      'uid' => $uid,
      'order_id' => $order->order_id,
      'order_numer' => $order->order_number,
      'sku' => $sku,
     );
     drupal_write_record('my_custom_table', $record);
    }
  }
?>

Hope this helps.

  • Thanks for the answer. I'll give this a go and get back to you. Looks like the sort of things I'm searching for. – Adam Nuttall Sep 5 '12 at 7:30

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.