2

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

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 字段(如帐单电话、电子邮件等)都会自动填充会议。

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

4

2 回答 2

2

更新( 第一个函数替换了错误的WC_Session方法)set()get()

这也适用于来宾用户。将您的代码替换为:

// 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 变量来存储客人提交的值,允许在未完成交易返回时在结帐时显示它。

于 2021-02-28T16:58:54.137 回答
0

update_order_review根据我的经验,当通过路径更新字段时,WooCommerce 会通过 AJAX 将表单数据保存在会话变量中。为了挂钩并存储我的自定义变量,我使用了以下内容:

add_action('woocommerce_checkout_update_order_review', 'custom_woocommerce_checkout_update_order_review');
function custom_woocommerce_checkout_update_order_review($post_data){

    // Convert $post_data string to array and clean it
    $post_arr = array();
    parse_str($post_data, $post_arr);
    wc_clean($post_arr);

    if(isset($post_arr['gst_no'])) {
        WC()->session->set('gst_no', $post_arr['gst_no']);
    }
}

还有一种更深入的方法来添加自定义结帐字段,以便它在传递给的 $data 中可用woocommerce_checkout_update_order_meta

add_filter('woocommerce_checkout_fields', 'custom_checkout_fields');
function custom_checkout_fields($fields){
    $fields['gst_no'] = array(
        'type'        => 'text',
        'default'     => WC()->session->get('gst_no')
    );
    return $fields;
}
于 2021-11-30T14:10:23.173 回答