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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建