将WooCommerce结帐自定义字段另存为用户元数据 [英] Save WooCommerce checkout custom field as user meta data

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

问题描述

我想将我的自定义注册字段添加到我的结帐页面表单中.

I want to add my custom registration field to my checkout page form.

我正在使用此代码将自定义字段添加到我的注册区域.

I'm using this code for adding custom field to my registration area.

顺便说一句,我将结帐字段用于我的注册字段中.

By the way, i'm using my checkout fields in to my registration fields.

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {

    $fields['billing']['shipping_tc'] = array(
        'label' => __('TC Kimlik No', 'woocommerce'),
        'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );
    
    return $fields;
}

我尝试了这段代码来更新用户元

And I tried this code for update user meta

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

没有错误,但是没有用...有人可以帮助我吗?

No errors but it doesn't work... Anyone can help me?

我正在使用此代码成功更新其他默认结帐值;

I'm succesfully updating other default checkout values with help this code;

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
    }
}

如何通过注册更新自定义结帐字段?

What can I do for update custom checkout fields by the registration?

这是我的注册页面.

推荐答案

主要错误是在结帐部分使用结帐字段,该字段的字段键以 shipping _ 开头...

The main error is to use a checkout field which field key is starting with shipping_ in the billing section…

另外,您最好使用复合挂钩 woocommerce_billing_fields 的钩子,它将为您做所有事情(因此,无需像WooCommerce那样将字段另存为用户元数据或订购商品元数据)

Also you should better use the hook the composite hook woocommerce_billing_fields which will do everything for you (so no need to save the field as user meta data or order item meta data as it's done by WooCommerce).

因此,唯一需要的代码替换将是(使用字段键 billing_identifier ,而不是混淆 shipping_tc )):

So the only required code replacement will be (with field key billing_identifier instead of confusing shipping_tc):

add_filter( 'woocommerce_billing_fields' , 'add_custom_billing_field' );
function add_custom_billing_field( $fields ) {
    $fields['billing_identifier'] = array(
        'label' => __('TC Kimlik No', 'woocommerce'),
        'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );

    return $fields;
}

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

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

该字段还将显示在我的帐户"中.地址>编辑帐单地址.

The field will additionally appear in My account > Addresses > Edit Billing address.

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

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