0

I am trying to write order line items from Woocommerce (in Wordpress) to a file and I don't know how to get that data.

Here is my hook and function for it:

add_action( 'woocommerce_new_order', 'helios_export',  1, 1  );
function helios_export( $order_id ) {

$order = wc_get_order( $order_id );
$order_data = $order->get_data();
$line_items = $order->get_items();

$fp = fopen('order.txt', 'w');
fwrite($fp, json_encode($line_items, JSON_PRETTY_PRINT));
fclose($fp);
}

This code doesn't write anything inside the file and I get no errors.

The $order_data object contains all the order data, but the line items show up empty there too:

line_items: []

So how do I get the items from the order?

Thank you!

Jacob
  • 45
  • 1
  • 11
  • 2
    Does this answer your question? [WooCommerce new order action - get order information](https://stackoverflow.com/questions/37868183/woocommerce-new-order-action-get-order-information) (Make sure to check out this answer in particular, https://stackoverflow.com/a/66813346/1427878) – CBroe Mar 24 '22 at 11:19
  • @CBroe The particular answer you posted solved the problem partially, but I can work with that. Thank you – Jacob Mar 24 '22 at 11:28

0 Answers0