du9757 2019-01-28 12:36
浏览 151
已采纳

在Woocommerce管理订单页面中保存订单商品自定义字段

I have add custom fields in back office order for each line products : Image

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

Can you please help me?

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 );
}
add_action( 'woocommerce_before_order_itemmeta', 'cfwc_create_custom_field' );
  • 写回答

1条回答 默认 最新

  • duangenshi9836 2019-01-28 16:46
    关注

    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_shop_order', 'save_order_item_custom_field_value');
    function save_order_item_custom_field_value( $post_id ){
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
            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] ) ) {
                wc_update_order_item_meta( $item_id, '_custom_field', sanitize_text_field( $_POST['cfield_oitem_'.$item_id] ) );
            }
        }
    }
    
    // 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;
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题