验证并保存Woocommerce中特定付款网关的其他结帐字段 [英] Validate and save additional checkout field for specific payment gateway in Woocommerce

查看:110
本文介绍了验证并保存Woocommerce中特定付款网关的其他结帐字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当客户选择BACS网关时,我需要显示一个文本输入字段,我希望将输入字段值附加到订单和电子邮件通知中.

I need to show an text input field when customers select BACS gateway and I would like the input field value to be appended to orders and email notifications.

我正在使用其他字段在Woocommerce中特定付款网关的结帐页面上回答代码,其中我已将选择字段更改为输入文本字段:

I am using Additional field on checkout for specific payment gateway in Woocommerce answer code where I have changed the select field to an input text field:

add_filter( 'woocommerce_gateway_description', 'gateway_bacs_custom_fields', 20, 2 );
function gateway_bacs_custom_fields( $description, $method_id ){
    //
     if( $method_id == 'bacs' ){

        ob_start(); // Start buffering

        echo '<div  class="bacs-fields" style="padding:10px 0;">';

        woocommerce_form_field( 'field_slug', array(
            'type'          => 'text',
            'label'         => __("Udfyld EAN", "woocommerce"),
            'class'         => array('form-row-wide'),
            'required'      => true,
                            ), '');
        echo '<div>';

        $description .= ob_get_clean(); // Append  buffered content
    }
    return $description;
}

它在显示该字段的结帐页面上运行良好.

It works fine on checkout page where it displays the field.

但是输入的文本值未保存到订单和电子邮件通知中.

But the inputted text value is not being saved to orders and email notifications.

如何在订单和电子邮件通知中保存并附加此输入的文本值?

How to save and append this inputted text value on orders and email notifications?

推荐答案

有很多遗漏的步骤,因为您使用的答案代码只是在BACS付款说明下的结帐中显示一个字段:

There is a lot of missing steps as the answer code you are using is just displaying a field in checkout under BACS payment description:

您需要的(仅在BACS是选定的支付方法):

  1. 验证字段
  2. 将输入的值保存到订单中
  3. 在收到的订单"和订单"视图(我的帐户)上显示字段值
  4. 在电子邮件通知上显示字段值
  5. 在管理员编辑订单页面中显示字段值

因此,您可以看到您要问的是很大的(太宽泛了),并且在第3、4和5点还需要一个新的问题,在这里您需要说出您想在哪里将其输出(位置).

So as you can see what you are asking is huge (too broad) and will require an additional new question for point 3, 4 and 5, where you will need to say where you want to output it (the location).

第1步和第2步的所有代码

All the code for steps 1 and 2:

add_filter( 'woocommerce_gateway_description', 'gateway_bacs_appended_custom_text_fields', 10, 2 );
function gateway_bacs_appended_custom_text_fields( $description, $payment_id ){
     if( $payment_id === 'bacs' ){

        ob_start(); // Start buffering

        echo '<div class="bacs-fields" style="padding:10px 0;">';

        woocommerce_form_field( 'udfyld_ean', array(
            'type'          => 'text',
            'label'         => __("Udfyld EAN", "woocommerce"),
            'class'         => array('form-row-wide'),
            'required'      => true,
        ), '');

        echo '<div>';

        $description .= ob_get_clean(); // Append  buffered content
    }
    return $description;
}


// Process the field (validation)
add_action('woocommerce_checkout_process', 'udfyld_ean_checkout_field_validation');
function udfyld_ean_checkout_field_validation() {
if ( $_POST['payment_method'] === 'bacs' && isset($_POST['udfyld_ean']) && empty($_POST['udfyld_ean']) )
    wc_add_notice( __( 'Please enter your "Udfyld EAN" number.' ), 'error' );
}

// Save "Udfyld EAN" number to the order as custom meta data
add_action('woocommerce_checkout_create_order', 'save_udfyld_ean_to_order_meta_data', 10, 4 );
function save_udfyld_ean_to_order_meta_data( $order, $data ) {
    if( $data['payment_method'] === 'bacs' && isset( $_POST['udfyld_ean'] ) ) {
        $order->update_meta_data( '_udfyld_ean', sanitize_text_field( $_POST['udfyld_ean'] ) );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.

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

要从 $order WC_Order对象获取此自定义字段值,您将使用:

To get this custom field value from $order the WC_Order object you will use:

$udfyld_ean = $order->get_meta('_udfyld_ean');

或者从 $order_id 中,可以使用WordPress get_post_meta()功能的订单ID:

Or from $order_id the order ID you can use WordPress get_post_meta() function:

$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );

字段验证(对于BACS作为选定的付款方式):

输入的字段值将保存到订单元数据(wp_postmeta表中的phpMyAdmin视图):

The inputted field value is saved to the order meta data (phpMyAdmin view in wp_postmeta table):

这篇关于验证并保存Woocommerce中特定付款网关的其他结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆