0

I am using woocommerce and I need to add an entry to a sperate database with order details and custom Fields.

I use the code snippits plugin but I need to know what functions to use to trigger the code when the order is marked completed and how to get the product details.

I think the easy way for me to keep this stable would be to have my function get the data and POST it to a script at the card system which would handle writing the data to the database. The only Fields I need are the Buyers Name, Phone, and Email; the SKU of the voucher; and the custom field (card_type).

To give a little more background we are selling passes to a local fire museum. The pass is a variable product with different quantities as different variations. the tracking system I made is a very simple PHP CRUD setup that records the card details and transactions to keep track of the balance. We have lots of different volunteers and I wanted to keep track of the vouchers created by woocommerce in the card tracking system too. That way I dont have to keep adding restricted users to woocommerce. I have to give more permission than I would like in order for them to see and redeem the vouchers.

Customer buys the voucher online, brings it in, and we give them the reusable card.

JpaytonWPD
  • 485
  • 1
  • 7
  • 22

1 Answers1

2

You can use your theme's functions.php as a trigger point.

Use the this hook woocommerce_order_status_completed:

add_action( 'woocommerce_order_status_completed', 'handle_order_completed');

The callback function will have a $order_id injected:

function handle_order_completed($order_id){...

Retrieve the information you need by first retrieving the order object itself:

wc_get_order($order_id)

Custom Fields

Order custom fields are simply Post custom fields. Therefore, you can can get them with the old trusty get_post_meta:

$orderMeta = get_post_meta($order_id, 'custom_order_meta_name_here');

Scriptonomy
  • 3,975
  • 1
  • 15
  • 23
  • Exelent, how do I find the custom meta fields? I know I can use `$order->billing_email` for the static fields. and the comment on my question helped me get the products into an array `$order->get_items()` Is there one for the custom fields? – JpaytonWPD Nov 21 '15 at 21:50
  • Thats perfect, Thanks! – JpaytonWPD Nov 21 '15 at 23:06