我已經創建了使用PODS.io WordPress插件用戶元以下的自定義字段:如何WooCommerce結帳自定義字段保存到用戶的元數據
- DATE_OF_BIRTH
- emergency_contact_name
- 關係
- emergency_phone
我已將這些字段添加到WooCommerce結帳中額外的信息,通過使用我的主題的functions.php
下面的代碼:
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在您的示例中未定義 – David