在Woocommerce订单编辑视图中显示自定义字段值

时间:2017-10-06 19:14:32

标签: php wordpress woocommerce custom-fields orders

我在here中有类似的内容:我不明白为什么不保存此自定义字段的值。这是我的代码:

add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );

function altri_campi( $fields ) {
$fields['billing']['codice_fiscale'] = array(
        'class'     => array('form-row-wide'),
        'label'     => __('Codice Fiscale', 'woocommerce'),
        'placeholder'   => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide')
    );

    return $fields;

}    

// like LoizTheAztec above
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['codice_fiscale'] ) ) {
            update_post_meta( $order_id, 'Codice Fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
        }
    }

    // then I'm expecting that custom field value will be saved somehow, but won't

    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_codice_fiscale', true ) . '</p>';
    }

元字段已正确保存和打印,但我无法在订单数据视图中添加自定义字段值。

我做错了什么以及如何在订单编辑视图中显示此自定义字段值?

否则,我希望在客户的结算和发货数据之后找到的文字无效,因为我只阅读了最新代码段的html部分。

1 个答案:

答案 0 :(得分:2)

以下是正确评论和解释的代码:

// Creating and displaying the custom checkout field in checkout page
add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );
function altri_campi( $fields ) {
    $fields['billing']['codice_fiscale'] = array(
        'class'     => array('form-row-wide'),
        'label'     => __('Codice Fiscale', 'woocommerce'),
        'placeholder'   => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide')
    );
    return $fields;
}

// Saving the custom checkout field value in the order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['codice_fiscale'] ) ){
        update_post_meta( $order_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );

        // get the customer ID
        $customer_id = get_post_meta( $order_id, '_customer_user', true );

        // Update customer user data
        update_user_meta( $customer_id, 'codice_fiscale', true );
    }
}

// Displaying the custom checkout field value in the order edit page (backend)
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
    $codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true );
    if( ! empty( $codice_fiscale ))
        echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . $codice_fiscale . '</p>';
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

  

经过测试和工作。您将进入后端订单编辑页面,例如(地址下方)

     

enter image description here