简体   繁体   English

在Woocommerce 3中在结帐和我的帐户上启用自定义字段

[英]Enable a Custom Field on checkout and on My account In Woocommerce 3

I created a some custom fields for Woocommerce in the check-out page. 我在结帐页面中为Woocommerce创建了一些自定义字段。 I was able to display them in WP admin area, email and invoices but not in the Front-End for User's Dashboard. 我可以在WP管理区域,电子邮件和发票中显示它们,但不能在“用户仪表板”的前端显示它们。

This is the code I used to create the field "CODICE SNEP" in my CHILD-THEM on function.php file: 这是我在function.php文件中的CHILD-THEM中用于创建“ CODICE SNEP”字段的代码:

// Create hook - CODICE SNEP
add_filter( 'woocommerce_checkout_fields' , 'codice_snep' );
function codice_snep ( $fields ) {
     $fields['billing']['codice_snep'] = array(
    'label'     => __('Codice Snep', 'woocommerce'),
    'placeholder'   => _x('Codice Snep', 'placeholder', 'woocommerce'),
    'required'  => true,
    'clear'     => true
     );

     return $fields;
}

add_action( 'woocommerce_checkout_update_order_meta', 'codice_snep_order_meta' );
function codice_snep_order_meta( $order_id ) {
    if ( ! empty( $_POST['codice_snep'] ) ) {
        update_post_meta( $order_id, 'Codice Snep', sanitize_text_field( $_POST['codice_snep'] ) );
    }
}

// Save the custom field 'codice_snep' 
add_action( 'woocommerce_save_account_details', 'save_codice_snep_account_details', 12, 1 );
function save_codice_snep_account_details( $user_id ) {
    // For Codice Snep
    if( isset( $_POST['codice_snep'] ) )
        update_user_meta( $user_id, 'codice_snep', sanitize_text_field( $_POST['codice_snep'] ) );


}

After that I show an error if you don't put the field: 之后,如果您不输入该字段,则会显示错误消息:

// Show error if you don't insert CODICE SNEP
add_action('woocommerce_checkout_process', 'required_codice_snep_checkout_field_process');
function required_codice_snep_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['codice_snep'] )
        wc_add_notice( __( 'Compila il campo Codice SNEP .' ), 'error' );
}

Than I show the code in the Admin Area of WC, emails and invoice: 比起我在WC的管理区域,电子邮件和发票中显示代码:

// Show field codice snep  in the Wc pdf invoices plugin
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_codice_snep', 10, 2 );
function wpo_wcpdf_codice_snep ($template_type, $order) {
    $document = wcpdf_get_document( $template_type, $order );
    if ($template_type == 'invoice') {
        ?>
        <tr>
            <th>Codice Snep:</th>
            <td><?php $document->custom_field('Codice Snep'); ?></td>
        </tr>
        <?php
    }
}

// Show CODICE SNEP in Back End Wp
add_action( 'woocommerce_admin_order_data_after_billing_address', 'codice_snep_order_meta_admin', 10, 1 );
function codice_snep_order_meta_admin($order){
    echo '<p><strong>'.__('Codice Snep').':</strong> ' . get_post_meta( $order->id, 'Codice Snep', true ) . '</p>';
}

// Show codoce snep in the order email

add_filter('woocommerce_email_order_meta_keys', 'my_custom_codice_snep_order_meta_keys');
function my_custom_codice_snep_order_meta_keys( $keys ) {
    $keys[] = 'Codice Snep';
    return $keys;
}

Everything seems to work until here. 一切似乎都可以进行到这里。

The problem now is to just show (must be not editable) this field ( codice_snep ) in the Dashboard front-end. 现在的问题是仅在Dashboard前端中显示(必须不可编辑)此字段( codice_snep )。

Right now I'm trying with this but I can only see the label without the value of the field. 目前,我正在尝试使用此方法,但只能看到没有字段值的标签。

// Show codice snep in the Dashboard of the user in front-end.
add_action( 'woocommerce_account_dashboard', 'codice_snep_order_dashboard', 12, 1 );
function codice_snep_order_dashboard ($order){
    echo '<p><strong>'.__('Codice Snep').':</strong> ' . get_post_meta( $order->id, 'Codice Snep', true ) . '</p>';
}

This is what happens: Field not showing on dashboard 这是发生了什么: 字段未显示在仪表板上

Can you help me to fix this problem in order to show the value that the user insert in the check-out? 您可以帮我解决此问题,以显示用户在结帐时插入的value吗?

The code you are using is a bit outdated and/or some hooks are deprecated (even if they come from Woocommerce documentation) . 您正在使用的代码有点过时和/或不建议使用某些挂钩(即使它们来自Woocommerce文档)

The code below uses different hooks. 下面的代码使用不同的钩子。 It allows to display this field and the related data (when it exist), in checkout and in My account > Adresses > Billing fields, without any need of additional validation code. 它允许在结帐和“我的帐户”>“地址”>“结算”字段中显示此字段和相关数据(如果存在),而无需其他验证代码。

Also when order is placed, the data is saved with one hook in the order and in the user meta data at the same time. 同样,在下订单时,数据将通过一个钩子同时保存在订单中和用户元数据中。 So the code is more compact and uses the right hooks with the new CRUD methods introduced in Woocommerce 3. 因此,代码更加紧凑,并 Woocommerce 3中引入的新CRUD方法配合使用了正确的钩子。

Your revisited code: 您重新访问的代码:

// Display a custom field on checkout and on My account > edit billing address
add_filter( 'woocommerce_billing_fields' , 'adding_billing_codice_snep', 20, 1 );
function adding_billing_codice_snep ( $fields ) {
     $fields['billing_codice_snep'] = array(
        'label'       => __('Codice Snep', 'woocommerce'),
        'placeholder' => _x('Codice Snep', 'placeholder', 'woocommerce'),
        'class'       => array('form-row-wide'),
        'required'    => true,
        'clear'       => true,
     );

     return $fields;
}

// Save the custom field data to the order meta data and to user meta data
add_action( 'woocommerce_checkout_create_order', 'codice_snep_order_meta', 20, 2 );
function codice_snep_order_meta( $order, $data ) {
    if ( isset( $_POST['billing_codice_snep'] ) && ! empty( $_POST['billing_codice_snep'] ) ) {
        $order->update_meta_data('_billing_codice_snep', sanitize_text_field( $_POST['billing_codice_snep'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_codice_snep', sanitize_text_field( $_POST['billing_codice_snep'] ) );
    }
}

// Order pages (frontend and admin): Display custom field "codice snep"
add_filter( 'woocommerce_order_details_after_order_table' , 'display_admin_order_meta_codice_snep', 20, 1 ); // Front
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta_codice_snep', 20, 1 ); // Admin
function display_admin_order_meta_codice_snep( $order ){
    $codice_snep = $order->get_meta('_billing_codice_snep', true );
    if( ! empty( $codice_snep ) ){
        $label = __('Codice Snep');
        if( is_admin() ){ // Admin
            echo '<p><strong>' . $label . ':</strong> ' . $codice_snep . '</p>';
        }
        else { // Front end: order view and Order received (thankyou)
            echo '<table class="woocommerce-table"><tbody><tr>
                <th>' . $label . ':</th><td>' . $codice_snep . '</td>
            </tr></tbody></table>';
        }
    }
}

// Email notifications: Display custom field "codice snep"
add_filter( 'woocommerce_email_order_meta_fields' , 'display_email_codice_snep', 20, 3 );
function display_email_codice_snep ( $fields, $sent_to_admin, $order ) {
    $codice_snep = $order->get_meta('_billing_codice_snep', true );
    if( ! empty( $codice_snep ) )
        $fields['codice_snep'] = array(
            'label' => __("Codice Snep"),
            'value' => $codice_snep,
        );
    return $fields;
}

// PDF Invoices: Display custom field "codice snep"
add_action( 'wpo_wcpdf_after_order_data', 'display_pdf_invoice_codice_snep', 20, 2 );
function display_pdf_invoice_codice_snep ($template_type, $order) {
    $document = wcpdf_get_document( $template_type, $order );
    if ($template_type == 'invoice') {
        echo '<tr>
            <th>' . __("Codice Snep") . ':</th>
            <td>' . $order->get_meta('_billing_codice_snep', true ) . '</td>
        </tr>';
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

Order view: 订单视图:

在此处输入图片说明

My account > adresses > Billing 我的帐户>地址>结算

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM