Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce get order key

I have orders in the format [domain]/checkout/order-received/[order_number]/key=[wc-order-key] - how do I get [wc-order-key]?

So far I've done:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);

function custom_process_order($order_id)
{
  $order = new WC_Order( $order_id );
  $myuser_id = (int)$order->user_id;
  $user_info = get_userdata($myuser_id);
  $items = $order->get_items();
  foreach ($items as $item)
  {
    $product_name = $item['name'];
    $product_id = $item['product_id'];
    $product_variation_id = $item['variation_id'];
    $product_description = get_post_meta($item['product_id'])->post_content
  }
  return $order_id;
}
like image 711
BKCapri Avatar asked Jun 07 '16 03:06

BKCapri


2 Answers

If i understand correctly, you need get order_key by order_id, is it correct? If so, you can just use WC_Order property:

$test_order = new WC_Order($order_id);
$test_order_key = $test_order->order_key;

Edited

As mentioned indextwo, since Woo 3.0 there new syntax:

$test_order = wc_get_product($order_id);
$test_order_key = $test_order->get_order_key();
like image 175
Alexey Svidentsov Avatar answered Oct 13 '22 00:10

Alexey Svidentsov


2018 updated answer

As WooCommerce 3 changed how property calls were made, the appropriate way to get the same information is:

$order = wc_get_order($order_id);

// Added a check to make sure it's a real order

if ($order && !is_wp_error($order)) {
    $order_key = $order->get_order_key();
}

Note that you can easily do the same in reverse: get an order ID from an order key:

$order_id = wc_get_order_id_by_order_key($order_key);
like image 37
indextwo Avatar answered Oct 13 '22 00:10

indextwo