如何将 WooCommerce 结帐自定义字段保存到用户元数据 [英] How to save WooCommerce checkout custom fields to user meta

查看:31
本文介绍了如何将 WooCommerce 结帐自定义字段保存到用户元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PODS.io Wordpress 插件为用户元创建了以下自定义字段:

  • 出生日期
  • emergency_contact_name
  • 关系
  • emergency_phone

我在我的主题的 functions.php 中使用以下代码在额外信息下将这些字段添加到 WooCommerce 结帐:

add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');函数 my_custom_checkout_field( $checkout ) {woocommerce_form_field( 'date_of_birth', array('类型' =>'文本','类' =>数组('我的字段类表单行宽'),'标签' =>__('出生日期'),'占位符' =>__('日/月/年'),), $checkout->get_value( 'date_of_birth' ));woocommerce_form_field( 'emergency_contact_name', array('类型' =>'文本','类' =>数组('我的字段类表单行宽'),'标签' =>__('紧急联络名字'),'占位符' =>__('联系人姓名'),), $checkout->get_value('emergency_contact_name'));woocommerce_form_field('关系',数组('类型' =>'文本','类' =>数组('我的字段类表单行宽'),'标签' =>__('紧急关系'),'占位符' =>__('妻夫'),), $checkout->get_value('relation'));woocommerce_form_field( 'emergency_phone', array('类型' =>'文本','类' =>数组('我的字段类表单行宽'),'标签' =>__('紧急电话'),'占位符' =>__('xxxx xxx xxx/xxxx xxxx'),), $checkout->get_value('emergency_phone'));}

错误检查:

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');函数 my_custom_checkout_field_process() {//检查是否设置,如果未设置则添加错误.如果(!$_POST['date_of_birth'])wc_add_notice( __( '请输入您的出生日期' ), '错误' );如果(!$_POST['emergency_contact_name'])wc_add_notice( __( '请输入您的紧急联系人姓名' ), '错误' );如果(!$_POST['relation'])wc_add_notice( __( '请输入您的紧急联系人与您的关系' ), '错误' );如果(!$_POST['emergency_phone'])wc_add_notice( __( '请输入您的紧急联系人的电话号码' ), '错误' );}

(希望)在结帐时更新用户元数据:

add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');函数 my_custom_checkout_field_update_order_meta( $order_id ) {如果 ( !empty( $_POST['date_of_birth'] ) ) {$dob = sanitize_text_field( $_POST['date_of_birth'] );update_user_meta( $current_user->ID, 'date_of_birth', $dob);}如果(!空($_POST['emergency_contact_name'])){update_user_meta( $user_id, 'emergency_contact_name', sanitize_text_field( $_POST['emergency_contact_name'] ) );}如果(!空($_POST['relation'])){update_user_meta( $user_id, 'relation', sanitize_text_field( $_POST['relation'] ) );}如果(!空($_POST['emergency_phone'])){update_user_meta( $user_id, 'emergency_phone', sanitize_text_field( $_POST['emergency_phone'] ) );}}

不幸的是,当我结帐时,用户元自定义字段没有更新.

我可以使用以下代码更新订单元自定义字段:

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');函数 my_custom_checkout_field_update_order_meta( $order_id ) {如果(!空($_POST['date_of_birth'])){update_post_meta( $order_id, '出生日期', sanitize_text_field( $_POST['date_of_birth'] ) );}如果(!空($_POST['emergency_contact_name'])){update_post_meta( $order_id, '紧急联系人姓名', sanitize_text_field( $_POST['emergency_contact_name'] ) );}如果(!空($_POST['relation'])){update_post_meta( $order_id, '紧急关系', sanitize_text_field( $_POST['relation'] ) );}如果(!空($_POST['emergency_phone'])){update_post_meta( $order_id, '紧急电话', sanitize_text_field( $_POST['emergency_phone'] ) );}}

但是,我们需要用户元数据中的自定义字段,而不是订单元数据.

您能看出在结帐到用户元数据时保存自定义字段的代码有什么问题吗?

谢谢.

解决方案

首先,您应该像这样添加自定义字段:(使用 woocommerce_checkout_fields 过滤器)

function reigel_woocommerce_checkout_fields( $checkout_fields = array() ) {$checkout_fields['order']['date_of_birth'] = array('类型' =>'文本','类' =>数组('我的字段类表单行宽'),'标签' =>__('出生日期'),'占位符' =>__('日/月/年'),'必需' =>真的,);返回 $checkout_fields;}add_filter('woocommerce_checkout_fields', 'reigel_woocommerce_checkout_fields');

添加 'required' 并将其设置为 true 将与您检查是否设置此字段的方式相同.(你的错误检查")

然后在您的 woocommerce_checkout_update_user_meta 中,第一个参数不是 $order_id 而是 $customer_id.你也应该知道第二个参数是$posted.$posted 包含 $_POST[] 数据.如果您执行了上面的代码,则包括您的自定义字段.

function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {如果 (isset($posted['date_of_birth'])) {$dob = sanitize_text_field( $posted['date_of_birth'] );update_user_meta( $customer_id, 'date_of_birth', $dob);}}add_action('woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2);

I've created the following custom fields for the user meta using the PODS.io Wordpress plugin:

  • date_of_birth
  • emergency_contact_name
  • relation
  • emergency_phone

I've added these fields to the WooCommerce checkout, under extra information, by using the following code in my theme's functions.php:

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    woocommerce_form_field( 'date_of_birth', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Date of Birth'),
        'placeholder'   => __('dd/mm/yyyy'),
        ), $checkout->get_value( 'date_of_birth' ));

    woocommerce_form_field( 'emergency_contact_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Emergency Contact Name'),
        'placeholder'   => __('contact name'),
        ), $checkout->get_value( 'emergency_contact_name' ));

    woocommerce_form_field( 'relation', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Emergency Relation'),
        'placeholder'   => __('wife/husband'),
        ), $checkout->get_value( 'relation' ));

    woocommerce_form_field( 'emergency_phone', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Emergency Phone'),
        'placeholder'   => __('xxxx xxx xxx / xxxx xxxx'),
        ), $checkout->get_value( 'emergency_phone' ));

}

Error checking:

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['date_of_birth'] )
        wc_add_notice( __( 'Please enter your date of birth' ), 'error' );
    if ( ! $_POST['emergency_contact_name'] )
        wc_add_notice( __( 'Please enter your Emergency Contact Name' ), 'error' );     
    if ( ! $_POST['relation'] )
        wc_add_notice( __( 'Please enter how your Emergency Contact is related to you' ), 'error' );
    if ( ! $_POST['emergency_phone'] )
        wc_add_notice( __( 'Please enter the phone number of your Emergency Contact' ), 'error' );              
}

(Hopefully) update the user meta upon checkout:

add_action( 'woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( !empty( $_POST['date_of_birth'] ) ) {
        $dob = sanitize_text_field( $_POST['date_of_birth'] );
        update_user_meta( $current_user->ID, 'date_of_birth', $dob);
    }
    if ( ! empty( $_POST['emergency_contact_name'] ) ) {
        update_user_meta( $user_id, 'emergency_contact_name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
    }
    if ( ! empty( $_POST['relation'] ) ) {
        update_user_meta( $user_id, 'relation', sanitize_text_field( $_POST['relation'] ) );
    }
    if ( ! empty( $_POST['emergency_phone'] ) ) {
        update_user_meta( $user_id, 'emergency_phone', sanitize_text_field( $_POST['emergency_phone'] ) );
    }           
}

Unfortunately, the user meta custom fields are not updated when I checkout.

I can update the order meta custom fields with the following code:

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['date_of_birth'] ) ) {
        update_post_meta( $order_id, 'Date Of Birth', sanitize_text_field( $_POST['date_of_birth'] ) );
    }
    if ( ! empty( $_POST['emergency_contact_name'] ) ) {
        update_post_meta( $order_id, 'Emergency Contact Name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
    }
    if ( ! empty( $_POST['relation'] ) ) {
        update_post_meta( $order_id, 'Emergency Relation', sanitize_text_field( $_POST['relation'] ) );
    }
    if ( ! empty( $_POST['emergency_phone'] ) ) {
        update_post_meta( $order_id, 'Emergency Phone', sanitize_text_field( $_POST['emergency_phone'] ) );
    }           
}

However, we need the custom fields in the user meta, not the order meta.

Can you see what is wrong with the code that saves custom fields upon checkout to the user meta?

Thanks.

解决方案

first, you should add your custom fields like this: (use woocommerce_checkout_fields filter)

function reigel_woocommerce_checkout_fields( $checkout_fields = array() ) {

    $checkout_fields['order']['date_of_birth'] = array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Date of Birth'),
        'placeholder'   => __('dd/mm/yyyy'),
        'required'      => true, 
        );

    return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'reigel_woocommerce_checkout_fields' );

adding 'required' and setting it to true will have the same effect with how you check if this field is set or not. (your "Error checking")

then in your woocommerce_checkout_update_user_meta, the first parameter is not $order_id but the $customer_id. You should know too that the second parameter is $posted. $posted contains the $_POST[] data. Including your custom fields if you did the code above.

function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
    if (isset($posted['date_of_birth'])) {
        $dob = sanitize_text_field( $posted['date_of_birth'] );
        update_user_meta( $customer_id, 'date_of_birth', $dob);
    }
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );

这篇关于如何将 WooCommerce 结帐自定义字段保存到用户元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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