要编辑可编辑的元数据并保存“WooCommerce管理员订单”,您可以使用以下代码示例:
// 添加自定义字段到管理员订单页面
add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_custom_order_meta_fields' );
function add_custom_order_meta_fields( $order ){
// 获取订单ID
$order_id = $order->get_id();
// 获取现有的订单元数据
$custom_meta = get_post_meta( $order_id, 'custom_meta', true );
// 输出自定义字段输入框
woocommerce_wp_text_input( array(
'id' => 'custom_meta',
'label' => __( 'Custom Meta', 'woocommerce' ),
'value' => $custom_meta,
'wrapper_class' => 'form-field-wide',
) );
}
// 保存自定义字段值
add_action( 'woocommerce_process_shop_order_meta', 'save_custom_order_meta_fields' );
function save_custom_order_meta_fields( $order_id ){
// 更新自定义字段值
if ( ! empty( $_POST['custom_meta'] ) ) {
update_post_meta( $order_id, 'custom_meta', sanitize_text_field( $_POST['custom_meta'] ) );
}
}
将上述代码添加到您的主题的functions.php
文件中或使用一个自定义插件。这将在“WooCommerce管理员订单”页面上添加一个名为“Custom Meta”的自定义字段。
请注意,上述代码仅适用于管理员订单页面。如果您还想在前端订单页面上显示和编辑这些元数据,您需要进行一些额外的开发。