在Woocommerce中,下面的代码检索特定的自定义订单项元数据,并在“管理”订单页面的“常规”区域下添加可编辑的自定义字段:
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' );
现在,我尝试使用以下代码保存自定义可编辑字段的值,但是一旦我按下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' );
我做错了什么?如何保存此自定义字段值?
任何帮助将不胜感激。
答案 0 :(得分:5)
您的代码中有一些错误和错误。请尝试以下操作:
// Output a custom editable field in backend edit order pages under general section
add_action( 'woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field', 12, 1 );
function editable_order_custom_field( $order ){
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
// Get "customer reference" from order item meta data
if( $item->get_meta('Your Reference') ){
// The "customer reference"
$item_value = $item->get_meta('Your Reference');
// We output a hidden field with the Item ID (to be able to update this order item meta data later)
echo '<input type="hidden" name="item_id_ref" value="' . $item_id . '">';
break; // We stop the loop
}
}
// Get "customer reference" from meta data (not item meta data)
$updated_value = $order->get_meta('_customer_ref');
// Replace "customer reference" value by the meta data if it exist
$value = $updated_value ? $updated_value : ( isset($item_value) ? $item_value : '');
// Display the custom editable field
woocommerce_wp_text_input( array(
'id' => 'customer_ref',
'label' => __("Customer Reference:", "woocommerce"),
'value' => $value,
'wrapper_class' => 'form-field-wide',
) );
}
// Save the custom editable field value as order meta data and update order item meta data
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
function save_order_custom_field_meta_data( $post_id, $post ){
if( isset( $_POST[ 'customer_ref' ] ) ){
// Save "customer reference" as order meta data
update_post_meta( $post_id, '_customer_ref', sanitize_text_field( $_POST[ 'customer_ref' ] ) );
// Update the existing "customer reference" item meta data
if( isset( $_POST[ 'item_id_ref' ] ) )
wc_update_order_item_meta( $_POST[ 'item_id_ref' ], 'Your Reference', $_POST[ 'customer_ref' ] );
}
}
代码进入活动子主题(或主题)的function.php文件中。经过测试并可以正常工作。