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

在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' );

图片转代码服务由CSDN问答提供 功能建议

我在每个行产品的后台订单中添加了自定义字段:

我的问题是我不知道如何保存这些字段 。

你能帮我吗?

  function cfwc_create_custom_field(){
 $ args = array(
'id'=  >'custom_text_field_title',
'label'=> __('自定义文字字段标题','cfwc'),
'class'=>'cfwc-custom-field',
'desc_tip'=  > true,
'描述'=> __('输入自定义文本字段的标题。','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.

    点赞 打赏 评论

相关推荐 更多相似问题