I am currently importing orders from a marketplace to Woocommerce using the Worpress API. The Marketplace Order ID is added in the Customer Note Field in the Woocommerce order upon import. The Woocommmerce Order number is auto assigned from Woocommerce. I would like to use a Code Snippet to change the Woocommerce order number to the marketplace order ID stored in the Wocommerce Order´s customer Note field when the order is imported to Woocommerce. After reviewing the WordPress Order API Docu, I am still not sure if I need to update the number or order_key to achieve this. Highlighted in yellow is the number I am looking to replace:
enter image description here
This is the Code Snippet I have, but that is not working:
`function update_woocommerce_order_key( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
// Get the customer note
$customer_note = $order->get_customer_note();
// Check if the customer note is not empty
if ( ! empty( $customer_note ) ) {
// Update the order key meta field
update_post_meta( $order_id, '_order_key', $customer_note );
// Update the order key in the order object
$order->set_order_key( $customer_note );
$order->save();
}
}
add_action( ‘woocommerce_api_create_order’, ‘update_woocommerce_order_key’, 10, 1 );
`
Any suggestions how to solve this?
I have tried the order_id, number, order_key upon import but nothing takes effect.
I would expect the Woocommerce Order Number be updated with the order number from the Custom Note.
LuxmedIQ Deutschland is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.