在Woocommerce Admin订单页面中保存订单项自定义字段

问题描述:

我在后台订单中为每种产品添加了自定义字段:
(来源: com-pac.ovh )

I have add custom fields in back office order for each line products :
(source: com-pac.ovh)

我的代码:


add_action( 'woocommerce_before_order_itemmeta', 'cfwc_create_custom_field' );
function cfwc_create_custom_field() {

    $args = array(
        'id' => 'custom_text_field_title',
        'label' => __( 'Custom Text Field Title', 'cfwc' ),
        'class' => 'cfwc-custom-field',
        'desc_tip' => true,
        'description' => __( 'Enter the title of your custom text field.', 'ctwc' ),
    );

    woocommerce_wp_text_input( $args );
}

我的问题是我不知道如何保存此字段.

My problem is that I don't know how to save this fields.

有帮助吗?

更新2021年(使用CRUD对象方法)

要添加和保存自定义字段以订购订单项",在管理员订单编辑页面中,您将使用类似以下内容的

To add and save a custom field to order "line items" in admin order edit pages you will use something like:

// Add a custom field
add_action( 'woocommerce_before_order_itemmeta', 'add_order_item_custom_field', 10, 2 );
function add_order_item_custom_field( $item_id, $item ) {
    // Targeting line items type only
    if( $item->get_type() !== 'line_item' ) return;

    woocommerce_wp_text_input( array(
        'id'            => 'cfield_oitem_'.$item_id,
        'label'         => __( 'Custom Text Field Title', 'cfwc' ),
        'description'   => __( 'Enter the title of your custom text field.', 'ctwc' ),
        'desc_tip'      => true,
        'class'         => 'woocommerce',
        'value'         => wc_get_order_item_meta( $item_id, '_custom_field' ),
    ) );
}

// Save the custom field value
add_action('save_post', 'save_order_item_custom_field_value', 10000, 2 );
function save_order_item_custom_field_value( $post_id, $post ){
    if ( 'shop_order' !== $post->post_type )
        return $post_id;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    if ( ! current_user_can( 'edit_shop_order', $post_id ) )
        return $post_id;

    $order = wc_get_order( $post_id );

    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        if( isset( $_POST['cfield_oitem_'.$item_id] ) ) {
            $item->update_meta_data( '_custom_field', sanitize_text_field( $_POST['cfield_oitem_'.$item_id] ) );
            $item->save();
        }
    }
    $order->save();
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

(可选)将新的元键/值隐藏在后端中

Optionally Keep the new meta key/value as hidden in backend

add_filter( 'woocommerce_hidden_order_itemmeta', 'additional_hidden_order_itemmeta', 10, 1 );
function additional_hidden_order_itemmeta( $args ) {
    $args[] = '_custom_field';
    return $args;
}

(可选)更改显示的元密钥标签

Optionally Change the displayed meta key label

add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
    // Change displayed label for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_custom_field' ) {
        $display_key = __("Some label", "woocommerce" );
    }
    return $display_key;
}