【问题标题】:How to save WooCommerce checkout custom fields to user meta如何将 WooCommerce 结帐自定义字段保存到用户元
【发布时间】:2016-04-18 22:58:02
【问题描述】:

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

  • date_of_birth
  • emergency_contact_name
  • 关系
  • emergency_phone

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

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

}

错误检查:

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

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

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'] ) );
    }           
}

很遗憾,我结帐时没有更新用户元自定义字段。

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

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'] ) );
    }           
}

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

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

谢谢。

【问题讨论】:

  • $userid 在您的示例中未定义

标签: wordpress woocommerce custom-fields


【解决方案1】:

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

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

添加'required' 并将其设置为true 将与您检查此字段是否设置的效果相同。 (您的“错误检查”)

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

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

【讨论】:

  • 这正是我需要的 Reigel。代码就像一个魅力。
  • Reigel,我已经发布了一个类似的问题here
  • Reigel 不确定您是否仍然处于活动状态,但如果是这样,我正在尝试修改此代码以在用户在结帐过程中创建帐户时工作。如果你能在这里看看我的问题,我将不胜感激:stackoverflow.com/questions/61369817/…
猜你喜欢
  • 2021-07-02
  • 2022-01-03
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 2023-03-05
  • 2018-07-01
相关资源
最近更新 更多