我测试了你的代码,只有一些小错误。您已经非常接近让它按您的预期工作了。所以你应该需要对以下代码进行一些更改:
1)删除了“值”,因为这个挂钩函数适用于结账并且不需要值(当您尝试从不存在的订单中获取它们时更不需要值)。
这将避免隐藏的错误,并在客户已经填写了先前购买中的字段时显示正确的值......
// Add custom Checkout billing fields
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields', 20, 1);
function add_woocommerce_billing_fields( $billing_fields ) {
$billing_fields['billing_birthday'] = array(
'type' => 'tel',
'label' => __('Datum rojstva'),
'placeholder' => __('dd/mm/yyyy', 'placeholder'),
'pattern' => __('\d{1,2}/\d{1,2}/\d{4}', 'pattern' ),
'class' => array('form-row-first'),
'required' => true,
'clear' => true
);
$billing_fields['billing_socialno'] = array(
'type' => 'tel',
'label' => __('Davčna številka'),
'placeholder' => _x('8-mestna številka', 'placeholder'),
'class' => array('form-row-last'),
'required' => false,
'clear' => true
);
return $billing_fields;
}
// Change field type to tel for woocommerce checkout
add_action( 'woocommerce_after_checkout_form', 'change_checkout_field_input_type');
function change_checkout_field_input_type() {
echo "<script>document.getElementById('billing_postcode').type = 'tel';</script>";
echo "<script>document.getElementById('billing_birthday').type = 'tel';</script>";
}
2)这里的按键错了,所以这就是字段值不显示的原因在管理订单编辑页面中。
It was 'birthday'
and 'socialno'
代替'billing_birthday'
and 'billing_socialno'
.
// Setting custom fields Keys/Labels pairs in admin edit order pages and allow edit this fields correctly.
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
$billing_fields['birthday'] = array( 'label' => __('Datum rojstva', 'woocommerce') );
$billing_fields['socialno'] = array( 'label' => __('Davčna številka', 'woocommerce') );
return $billing_fields;
}
3)正确获取缺失的字段值显示在订单编辑页面。
// Get the field values to be displayed in admin Order edit pages
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);
function add_woocommerce_order_fields($address, $order ) {
$address['billing_birthday'] = get_post_meta( $order->get_id(), '_billing_birthday', true );
$address['billing_socialno'] = get_post_meta( $order->get_id(), '_billing_socialno', true );
return $address;
}
4)其他未更改的挂钩函数:
//Doda user meta v backend profil
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_customer_meta_fields($fields) {
if (isset($fields['billing']['fields'])) {
$fields['billing']['billing_birthday'] = array(
'label' => __('Datum rojstva', 'woocommerce'),
'description' => 'Pa kaj bo končno ratalo memo milo?'
);
$fields['billing']['billing_socialno'] = array(
'label' => __('Davčna številka', 'woocommerce'),
'description' => ''
);
}
return $fields;
}
add_filter( 'woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);
function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
if ($type_to_load == 'billing') {
$customer_data[$type_to_load . 'billing_birthday'] = get_user_meta($user_id, $type_to_load . 'billing_birthday', true);
$customer_data[$type_to_load . 'billing_socialno'] = get_user_meta($user_id, $type_to_load . 'billing_socialno', true);
}
return $customer_data;
}
//add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);
function add_woocommerce_formatted_address_replacements($replace, $args) {
$replace['{billing_birthday}'] = !empty($args['billing_birthday']) ? 'Datum rojstva' . $args['billing_birthday'] : '';
$replace['{billing_socialno}'] = !empty($args['billing_socialno']) ? 'Davčna številka' . $args['billing_socialno'] : '';
return $replace;
}
add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);
function add_woocommerce_localisation_address_formats($formats) {
$formats['default'] = $formats['default'] . "\n{billing_birthday}\n{billing_socialno}";
return $formats;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。
电子邮件通知- 显示自定义字段(及其标签)。
覆盖 Woocommerce 模板emails/email-addresses.php
通过您活跃的子主题:
该模板可以通过复制来覆盖:
plugin/woocommerce/templates/emails/email-addresses.php
To yourtheme/woocommerce/emails/email-addresses.php
…
官方文档:模板结构和通过主题覆盖模板 https://docs.woocommerce.com/document/template-structure/
您将插入第 34 行之后 (就在计费电话之后)下列:
<?php
// Billing birthday
$billing_birthday = get_post_meta($order->get_id(), '_billing_birthday', true );
echo $billing_birthday ? '<br/>'.__('Datum rojstva', 'woocommerce').': '.$billing_birthday : '';
// Billing socialno
$billing_socialno = get_post_meta($order->get_id(), '_billing_socialno', true );
echo $billing_socialno ? '<br/>'.__('Davčna številka', 'woocommerce').': '.$billing_socialno : '';
?>
经过测试并有效。