In Woocommerce, the code below retrieves specific custom order item meta data and adds an editable custom field in the admin edit orders pages, under general section area:
function editable_order_meta_general( $order_id ){
$order = wc_get_order( $order_id );
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
$yourref = $item->get_meta('Your Reference');
}
?>
<br class="clear" />
<h4>Customer Details <a href="#" class="edit_address">Edit</a></h4>
<div class="address">
<p><strong>Customer Reference:</strong> <?php echo $yourref ?></p>
</div>
<div class="edit_address"><?php
woocommerce_wp_text_input( array(
'id' => 'Your Reference',
'label' => 'Customer Ref:',
'value' => $yourref,
'wrapper_class' => 'form-field-wide'
) );
?></div>
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'editable_order_meta_general' );
Now I'm trying to save the custom editable field value with the following code, but it doesn't save anything once I hit update.
function save_general_detail_metas( $ord_id ){
update_post_meta( $ord_id, 'Your Reference', wc_clean( $_POST[ 'Your Reference' ] ) );
// wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
}
add_action( 'woocommerce_process_shop_order_meta', 'save_general_detail_metas' );
What I am doing wrong? How this custom field value coud be saved?
Any help would be much appreciated.