I want to add two types of metadata to an order and its order_items when it is created. I tried some things but it is not working.
The first I want to add is $order->get_date_created(); as meta data to the order.
// Add order date as order meta data
add_action( 'woocommerce_checkout_create_order', 'add_order_date_to_order_meta_data', 10, 2 );
function add_order_date_to_order_meta_data( $order, $data ) {
$order_date = $order->get_date_created();
$order->update_meta_data( 'order_created', $order_date );
}
The second is $order->get_id(); that I want to add to both the order itself as to the order_items
// Add order number as order meta data
add_action( 'woocommerce_checkout_create_order', 'add_order_number_to_order_meta_data', 10, 2 );
function add_order_number_to_order_meta_data( $order, $data ) {
$order_id = $order->get_id();
$order->update_meta_data( 'order_number', $order_id );
}
I am blank about about how to add it to the order lines. No clue on how to do that. I found the following but I believe that is adding information before a order is created?
add_action( 'woocommerce_checkout_create_order_line_item', 'add_meta_to_line_item', 20, 4 );
function add_meta_to_line_item( $item, $cart_item_key, $values, $order )
{
$_p = $item->get_product();
$key = $order->get_id();
if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
$item->add_meta_data( $key, true);
}
Then after I created these meta fields, I also want to add them to a column in the order overview on the front-end.
/**
* Adds a new column to the "My Orders" table in the account.
*
* @param string[] $columns the columns in the orders table
* @return string[] updated columns
*/
function metavalue_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add meta_value after order status column
if ( 'order-status' === $key ) {
$new_columns['order_number'] = __( 'Order Number', 'textdomain' );
$new_columns['order_created'] = __( 'Order Created', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'metavalue_add_my_account_orders_column' );
I hope someone can help me in the right direction.