1

I'm using the woocommerce_checkout_create_order_line_item action to update/add some order item meta data.

At the same time, within the same function, I want to use wc_get_order_item_meta to get a different meta value from the same item but I can't get it to work within this action hook.

I think the issue is that I'm not managing to get the $item_id needed to use wc_get_order_item_meta.

Am I going about this in the wrong way?

I've tried the method here How to get order items ids to get some product meta data?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Luke Seall
  • 525
  • 1
  • 4
  • 21
  • raju_eww actually solved the issue. The comments on his answer were moved to chat but I can't seem to access it anymore for some reason. I haven't got time to feedback on your answer right now as I am no longer working on this specific issue but may revisit it at some point. – Luke Seall Jul 04 '18 at 14:13

1 Answers1

2

Below code will return all details of order with order item meta.just take those values in count which you want to use remove others.

    function action_woocommerce_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) { 
        $order_id = $order->get_order_number();
        $order = wc_get_order( $order_id );
        $order_data = $order->get_data();

    //Order Data and order item metas

        $order_id = $order_data['id'];
        $order_parent_id = $order_data['parent_id'];
        $order_status = $order_data['status'];
        $order_currency = $order_data['currency'];
        $order_version = $order_data['version'];
        $order_payment_method = $order_data['payment_method'];
        $order_payment_method_title = $order_data['payment_method_title'];
        $order_payment_method = $order_data['payment_method'];
        $order_payment_method = $order_data['payment_method'];

        ## Creation and modified WC_DateTime Object date string ##

       // Using a formated date ( with php date() function as method)
       $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
       $order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s');

       // Using a timestamp ( with php getTimestamp() function as method)
       $order_timestamp_created = $order_data['date_created']->getTimestamp();
       $order_timestamp_modified = $order_data['date_modified']->getTimestamp();


       $order_discount_total = $order_data['discount_total'];
       $order_discount_tax = $order_data['discount_tax'];
       $order_shipping_total = $order_data['shipping_total'];
       $order_shipping_tax = $order_data['shipping_tax'];
       $order_total = $order_data['cart_tax'];
       $order_total_tax = $order_data['total_tax'];
       $order_customer_id = $order_data['customer_id']; // ... and so on

       ## BILLING INFORMATION:

       $order_billing_first_name = $order_data['billing']['first_name'];
       $order_billing_last_name = $order_data['billing']['last_name'];
       $order_billing_company = $order_data['billing']['company'];
       $order_billing_address_1 = $order_data['billing']['address_1'];
       $order_billing_address_2 = $order_data['billing']['address_2'];
       $order_billing_city = $order_data['billing']['city'];
       $order_billing_state = $order_data['billing']['state'];
       $order_billing_postcode = $order_data['billing']['postcode'];
       $order_billing_country = $order_data['billing']['country'];
       $order_billing_email = $order_data['billing']['email'];
       $order_billing_phone = $order_data['billing']['phone'];

       ## SHIPPING INFORMATION:

       $order_shipping_first_name = $order_data['shipping']['first_name'];
       $order_shipping_last_name = $order_data['shipping']['last_name'];
       $order_shipping_company = $order_data['shipping']['company'];
       $order_shipping_address_1 = $order_data['shipping']['address_1'];
       $order_shipping_address_2 = $order_data['shipping']['address_2'];
       $order_shipping_city = $order_data['shipping']['city'];
       $order_shipping_state = $order_data['shipping']['state'];
       $order_shipping_postcode = $order_data['shipping']['postcode'];
      $order_shipping_country = $order_data['shipping']['country'];
     }; 

     //To get Order item metas please use below code
   foreach ($order->get_items() as $item_key => $item_values):

    ## Using WC_Order_Item methods ##

    // Item ID is directly accessible from the $item_key in the foreach loop or
    $item_id = $item_values->get_id();

    ## Using WC_Order_Item_Product methods ##

    $item_name = $item_values->get_name(); // Name of the product
    $item_type = $item_values->get_type(); // Type of the order item ("line_item")

    $product_id = $item_values->get_product_id(); // the Product id
    $wc_product = $item_values->get_product(); // the WC_Product object
    ## Access Order Items data properties (in an array of values) ##
    $item_data = $item_values->get_data();

    $product_name = $item_data['name'];
    $product_id = $item_data['product_id'];
    $variation_id = $item_data['variation_id'];
    $quantity = $item_data['quantity'];
    $tax_class = $item_data['tax_class'];
    $line_subtotal = $item_data['subtotal'];
    $line_subtotal_tax = $item_data['subtotal_tax'];
    $line_total = $item_data['total'];
    $line_total_tax = $item_data['total_tax'];

  endforeach;

    // add the action 
    add_action( 'woocommerce_checkout_create_order_line_item', 'action_woocommerce_checkout_create_order_line_item', 10, 4 ); 
raju_odi
  • 1,433
  • 13
  • 29
  • I get an internal server error at checkout with this. It seems to be due to first 3 lines in the function. $order_id = $order->get_order_number(); $order = wc_get_order( $order_id ); $order_data = $order->get_data(); – Luke Seall May 16 '18 at 07:25
  • $order is already passed to the function isn't it? So do we need to use wc_get_order again? – Luke Seall May 16 '18 at 07:27
  • $order is object only so we need to get order id from $order then we can use this id to get item meta – raju_odi May 16 '18 at 07:28
  • Ok. any idea why its giving a server error though? If I remove first 3 lines I get no error. – Luke Seall May 16 '18 at 07:31
  • let me test the code again and let you know the issue – raju_odi May 16 '18 at 07:41
  • you want order item meta before order is placed right? – raju_odi May 16 '18 at 09:19
  • then you could not get order id or order meta data on checkout but you could get cart detail on that page – raju_odi May 16 '18 at 10:31
  • this is how you get cart detail : – raju_odi May 16 '18 at 10:32
  • add_action('woocommerce_checkout_process', 'wh_getCartItemBeforePayment', 10); function wh_getCartItemBeforePayment() { $items = WC()->cart->get_cart(); foreach ($items as $item => $values) { $_product = $values['data']->post; $product_title = $_product->post_title; $qty = $values['quantity']; $price = get_post_meta($values['product_id'], '_price', true); } } – raju_odi May 16 '18 at 10:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171143/discussion-between-luke-seall-and-raju-eww). – Luke Seall May 16 '18 at 11:11