在 Woocommerce 管理订单页面中保存订单项目自定义字段 [英] Save Order item custom field in Woocommerce Admin order pages

查看:25
本文介绍了在 Woocommerce 管理订单页面中保存订单项目自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后台订单中为每个产品线添加了自定义字段:
(来源: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.

有什么帮助吗?

推荐答案

Update 2021 (使用 CRUD 对象方法)

Update 2021 (using CRUD Objects methods)

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

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;
}

这篇关于在 Woocommerce 管理订单页面中保存订单项目自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆