如何用以前输入的值(如默认的WooCommerce结帐字段)填充自定义结帐字段? [英] How to fill custom checkout field with previously entered value, like default WooCommerce checkout fields?

查看:59
本文介绍了如何用以前输入的值(如默认的WooCommerce结帐字段)填充自定义结帐字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码添加了一个自定义字段:

I have added a custom field using the below code:

add_action( 'woocommerce_before_order_notes', 'bbloomer_add_custom_checkout_field' );
function bbloomer_add_custom_checkout_field( $checkout ) { 
   $current_user = wp_get_current_user();
   $saved_gst_no = $current_user->gst_no;
   
   woocommerce_form_field( 'gst_no', array(        
       'type'        => 'text',        
       'class'       => array( 'form-row-wide' ),        
       'label'       => 'GST Number',        
       'placeholder' => 'GST Number',        
       'required'    => true
       //'default'   => $saved_gst_no,        
   ), $checkout->get_value( 'gst_no' ) ); 
}

在GST编号字段(自定义结帐字段)中输入任何值,然后通过点击下订单"进入付款屏幕,按钮并返回到未完成交易的结帐页面,会话中的所有默认woocommerce字段(如计费电话,电子邮件等)都会自动填充.

On entering any value in GST Number field (custom checkout field), then going to payment screen by clicking "Place order" button and returning to checkout page without completing the transaction, all default woocommerce fields like billing phone, email etc are auto filled from the session.

但是,通过以上代码添加的自定义字段始终为空白.如何自动为来宾用户在自定义字段中填充先前输入的值,类似于自动填充默认woocommerce字段的方式?

However, the custom field added via above code is always blank. How to get the previously entered value auto-filled in the custom field for guest users, similar to the way default woocommerce fields are auto-filled?

推荐答案

已更新 (已替换错误的 WC_Session 方法 set()在第一个函数上使用 get())

Updated (replaced wrong WC_Session method set() with get() on the first function)

这也将适用于来宾用户.将代码替换为:

This will work for guest users too. Replace your code with:

// Display checkout custom field
add_action( 'woocommerce_before_order_notes', 'add_custom_checkout_field' );
function add_custom_checkout_field( $checkout ) { 
    $key_field = 'gst_no';
       
    woocommerce_form_field( $key_field, array(        
        'type'        => 'text',        
        'class'       => array( 'form-row-wide' ),        
        'label'       => __('GST Number'),        
        'placeholder' => __('GST Number'),        
        'required'    => true
        //'default'   => $saved_gst_no,        
    ), $checkout->get_value($key_field) ? $checkout->get_value($key_field) : WC()->session->get($key_field) ); 
}

// Save checkout custom field value in a WC_Session variable 
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order', 10, 2 );
function action_checkout_create_order( $order, $data  ) {
    $key_field = 'gst_no';
    
    if( isset($_POST[$key_field]) ) {
        WC()->session->set($key_field, sanitize_text_field($_POST[$key_field]));
    }
}

// Save checkout custom field value as user meta data
add_action( 'woocommerce_checkout_update_customer', 'action_checkout_update_customer', 10, 2 );
function action_checkout_update_customer( $customer, $data  ) {
    $key_field = $key_field;
    
    if( isset($_POST['gst_no']) ) {
        $customer->update_meta_data($key_field, sanitize_text_field($_POST[$key_field]));
    }
}

注意:使用WC_Session变量存储来宾的已提交值,以便在退房时在结账时显示该值而无需完成交易.

Note: Uses a WC_Session variable to store the submitted value for guests, allowing to display it on checkout when returning without completing the transaction.

这篇关于如何用以前输入的值(如默认的WooCommerce结帐字段)填充自定义结帐字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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