2
votes

Using 'before_checkout' hook , im doing some calculations with the cart item data which is before payment. I need to send those data to order item meta in another hook like 'woocommerce_thankyou' after payment is done.

Is there a way to relate the cart items and its corresponding order which is created?

2

2 Answers

4
votes

There is hook for add item meta for to be add to order so you can relate cart items and its corrosponding order:

add_action('woocommerce_add_order_item_meta', 'add_order_item_meta_after_order', 10, 3);
function add_order_item_meta_after_order($item_id, $values, $cart_item_key) {       
    wc_add_order_item_meta($item_id, '_your_key_in_order', $values, true);
}

May be useful to you.

0
votes

Here is the code you can use to get the cart items in the order 'woocommerce_thankyou' hook.

global $woocommerce;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach ($items as $pid) {
   $product_ids = $pid['product_id']; // and so on
}