![](/img/trans.png)
[英]Required custom WooCommerce checkout fields don't validate entered value
[英]How to fill custom checkout field with previously entered value, like default WooCommerce checkout fields?
我使用以下代碼添加了一個自定義字段:
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 字段,如計費電話、email 等都是自動填充的session。
但是,通過上述代碼添加的自定義字段始終為空白。 如何在訪客用戶的自定義字段中自動填充先前輸入的值,類似於自動填充默認 woocommerce 字段的方式?
更新(在第一個函數上用get()
替換了錯誤WC_Session
方法set()
)
這也適用於來賓用戶。 將您的代碼替換為:
// 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 變量來存儲客人提交的值,允許在未完成交易返回時在結帳時顯示它。
根據我的經驗,當通過update_order_review
路徑更新字段時,WooCommerce 通過 AJAX 將表單數據保存在 session 變量中。 為了掛鈎並存儲我的自定義變量,我使用了以下內容:
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']);
}
}
還有一種更深入的方法來添加自定義結帳字段,以便它在傳遞給woocommerce_checkout_update_order_meta
的 $data 中可用:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.