0

I am trying to create custom meta data in the wp_woocommerce_order_itemmeta table. But to create the data I want to store as meta data requires I know the product_id of the order item that is created at the Woocommerce hook woocommerce_checkout_create_order_line_item.

I have succeeded in creating a meta record with a value from a session variable but my code fails when I try to get the order item's product_id. I would greatly appreciate a solution for this.

So the following code works by creating a record in wp_woocommerce_order_itemmeta table with meta_key of "_raw_location_id'

add_action('woocommerce_checkout_create_order_line_item', 'raw_order_item_meta', 20, 2);
function raw_order_item_meta( $order ) {
    $order->update_meta_data( '_raw_location_id', $_SESSION['location_id'] );
}

So then I "enhanced" the code to create one more record but with the meta_key value of $product_id. In adding the code I thought necessary the checkout does not complete so I never get to the thank you page. The checkout page is re-displayed with no changes in the database. I did meticulous testing by adding one line of code at a time. I have number the lines of code below for easy reference.

I found that adding line "3" causes the error. So I have not been able to verify if lines 4 & 5 work or not and I took the code from another post so I can't even say I understand the syntax of line 5 I am just hoping it works as advertised. I think my first obstacle is line 3. Why is this not working? I just want to know what the product_id is of the order item being processed.

  1. add_action('woocommerce_checkout_create_order_line_item', 'raw_order_item_meta', 20, 2);
  2. function raw_order_item_meta( $order ) {

  3. $items = $order->get_items();

  4. foreach ( $items as $item_id => $item ) {
  5. $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();

  6. $order->update_meta_data( '_raw_location_id', $_SESSION['location_id'] );

  7. $order->update_meta_data( '_raw_wc_product', $product_id );

  8. }

  9. }
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Dave Lovely
  • 67
  • 1
  • 7

1 Answers1

2

You are using woocommerce_checkout_create_order_line_item in a wrong way as the hook arguments are wrong in your code. The correct code is:

add_action('woocommerce_checkout_create_order_line_item', 'add_order_item_custom_meta', 10, 4 );
function add_order_item_custom_meta( $item, $cart_item_key, $cart_item, $order ) {
    $item->update_meta_data( '_raw_location_id', $_SESSION['location_id'] );
}

So as you can see you don't need to loop through order items (as $item s the current order item) and your 2nd code will be:

add_action('woocommerce_checkout_create_order_line_item', 'add_order_item_custom_meta', 10, 4 );
function add_order_item_custom_meta( $item, $cart_item_key, $cart_item, $order ) {
    $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();

    $item->update_meta_data( '_raw_location_id', $_SESSION['location_id'] );
    $item->update_meta_data( '_raw_product_id', $product_id );
}

or the same thing in an other way:

add_action('woocommerce_checkout_create_order_line_item', 'add_order_item_custom_meta', 10, 4 );
function add_order_item_custom_meta( $item, $cart_item_key, $cart_item, $order ) {
    $product = $item->get_product(); // The WC_Product instance Object

    $item->update_meta_data( '_raw_location_id', $_SESSION['location_id'] );
    $item->update_meta_data( '_raw_product_id', $product->get_id() ); // Set the product ID 
}

Code goes in functions.php file of your active child theme (or active theme). It should work.

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Loic thank you so much! My follow-up question is how can I learn about using these hooks in general. How do I know what variables are available at any given point and what the variables are. For instance $cart_item_key I have not seen before so how am I supposed to know this? The argument $order I see is an instance of the WC_Order object but how do I know what variables are contained in the wc_order object? If I don't know that I can't get data out of it. I have looked at a thousand posts and still can't find a good tutorial. Anyway, I couldn't be more grateful for your response. Thanks! – Dave Lovely Jun 07 '19 at 22:55