I need to add a link with the order ID and a password to the order item metadata.
Currently, I am adding successfully other info to the item metadata with the woocommerce_checkout_create_order_line_item
action, but when this action is running the order ID is not accessible yet (?).
Can I do this in another way, so the link will be added before the order is saved and a notification to the customer is sent?
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( ! isset( $values['test_title'] ) ) { return; }
// add test info as order item metadata
if( ! empty( $values['test_title'] ) ) {
$test_pw = wp_generate_password(); // generate a password
$item->update_meta_data( '_test_id', $values['test_id'] ); // add test id
$item->update_meta_data( '_test_password', $test_pw ); // add test access password
$item->update_meta_data( 'Access link', // add test access permalink
get_permalink( $values['test_id'] ) . '?order=' . $order->get_id() . '&pw=' . $test_pw );
}
}