In WooCommerce, when creating and saving a manual order via Admin, I am trying to replace the order currency value with a custom meta data value (which meta key is _wcj_order_currency)
Here are 2 screenshots of the related meta data (key/value pairs):
So I would like to replace the order currency EUR to the custom currency USD from _order_currency meta key on save.
References I used:
My code attempt:
// Saving (Updating) or doing an action when submitting
add_action( 'save_post', 'update_order_custom_field_value' );
function update_order_custom_field_value( $post_id ){
// Only for shop order
// if ( 'shop_order' != $_POST[ 'post_type' ] )
if ( 'shop_order')
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
return $post_id;
//Up to above is fine for Admin order
// Updating custom field data
if( isset( $_POST['_wcj_order_currency'] ) ) {
$order = wc_get_order( $post_id );
// Replacing and updating the value
update_post_meta( $post_id, '_order_currency', $_POST['_wcj_order_currency'] );
}}
// Testing output in order edit pages (below billing address):
//This displays the existing values well
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_order_custom_field_value' );
function display_order_custom_field_value( $order ){
echo '<p><strong>'.__('Order Currency').':</strong> <br/>' . get_post_meta( $order->get_id(), '_order_currency', true ) . '</p>';
echo '<p><strong>'.__('Booster Order Currency').':</strong> <br/>' . get_post_meta( $order->get_id(), '_wcj_order_currency', true ) . '</p>';
}
Testing output in order edit pages (below billing address). The code works well.
But I am unable to make it work and update the order currency on order creation.
Any help on this is welcome.


