我的网站在 Woocommerce 的
/checkout
页面中有自定义字段是:Link (Facebook/Zalo/Telegram)
,但我在激活 HPOS 时遇到问题,当单击“同步”按钮并激活 HPOS 的兼容模式时,它将丢失字段:Link (Facebook/Zalo/Telegram)
,我可以'单击“同步 xxx 订单”按钮后,在任何订单中都找不到此内容。
我的代码是:
add_action('woocommerce_after_checkout_billing_form', 'custom_checkout_field');
function custom_checkout_field($checkout)
{
$key_field = 'fbhoaczalo';
echo '<div id="custom_checkout_field">';
woocommerce_form_field('fbhoaczalo', array(
'type' => 'textarea',
'id' => 'fb_zalo',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => ('<strong style="color: #ffdf30;font-size: 120%;">Your contact [ <code>Facebook</code>, <code>Zalo</code>, <code>Telegram</code> ] :</strong>') ,
'required' => true,
'placeholder' => ('This field is require') ,
) ,
$checkout->get_value($key_field) ? $checkout->get_value($key_field) : WC()->session->get($key_field) );
echo '</div>';
}
add_action('woocommerce_checkout_process', 'customised_checkout_field_process');
// 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 = 'fbhoaczalo';
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['fbhoaczalo']) ) {
$customer->add_meta_data($key_field, sanitize_text_field($_POST[$key_field]));
}
}
function customised_checkout_field_process()
{
// Show an error message if the field is not set.
if (!$_POST['fbhoaczalo']) {
wc_add_notice(__('This field is not blank. Please type your contact') , 'error');
}
}
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
function custom_checkout_field_update_order_meta($order_id)
{
if (!empty($_POST['fbhoaczalo'])) {
update_post_meta($order_id, 'fbhoaczalo',sanitize_text_field($_POST['fbhoaczalo']));
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'codeastrology_save_new_checkout_field' );
function codeastrology_save_new_checkout_field( $order_id ) {
if ( $_POST['fbhoaczalo'] ) update_post_meta( $order_id, '_fbhoaczalo', esc_attr( $_POST['fbhoaczalo'] ) );
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'codeastrology_show_new_checkout_field_order', 10, 1 );
function codeastrology_show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_fbhoaczalo', true ) ) echo '<p><strong>Link Facebook/Zalo/Tele:</strong> ' . get_post_meta( $order_id, '_fbhoaczalo', true ) . '</p>';
}
add_action( 'woocommerce_email_after_order_table', 'codeastrology_show_new_checkout_field_emails', 20, 4 );
function codeastrology_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_fbhoaczalo', true ) ) echo '<p><strong>Link Facebook/Zalo/Tele:</strong> ' . get_post_meta( $order->get_id(), '_fbhoaczalo', true ) . '</p>';
}
这将在
https://mysite/checkout/
页面中显示如下:
当用户填写此信息时,在订单管理中,我可以看到该字段并了解客户的联系方式,如下所示:
一切工作都很完美,但问题是当我在 WooCommerce 中打开功能时:
HPOS
。
勾选:Enable compatibility mode (synchronizes orders to the posts table).
,然后单击 Sync 2.394 pending orders
中的 There are 2394 orders pending sync. You can switch order data storage only when the posts and orders tables are in sync.
按钮。
当我单击任何订单时,字段
Link Facebook/Zalo/Tele
会丢失。我找不到此字段,因此如果订单有任何问题,我无法联系我的客户。
有什么建议在将挂单同步到 HPOS 后保留此自定义字段 (
Link Facebook/Zalo/Tele
)?
高性能订单存储 (HPOS) 需要专门使用 WooCommerce CRUD 方法,但不再需要使用 WordPress 帖子元功能,因为 WooCommerce 使用自己的数据库表。
此外,您的代码中存在一些错误。
尝试用以下代码替换您的代码:
// Add a custom checkout field
add_action('woocommerce_after_checkout_billing_form', 'custom_checkout_field');
function custom_checkout_field( $checkout ) {
$key_field = 'fbhoaczalo';
echo '<div id="custom_checkout_field">';
woocommerce_form_field($key_field, array(
'type' => 'textarea',
'id' => 'fb_zalo',
'class' => array('my-field-class form-row-wide'),
'label' => '<strong style="color: #ffdf30;font-size: 120%;">Your contact [ <code>Facebook</code>, <code>Zalo</code>, <code>Telegram</code> ] :</strong>',
'required' => true,
'placeholder' => __('This field is required'),
), $checkout->get_value($key_field) );
echo '</div>';
}
// Custom checkout field validation
add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');
function custom_checkout_field_validation(){
$key_field = 'fbhoaczalo';
if ( isset($_POST[$key_field]) && empty($_POST[$key_field])) {
wc_add_notice(__('Please fill in your contact, as this is a required field') , 'error');
}
}
// Save custom checkout field value as order metadata
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field_as_order_meta', 10, 2 );
function save_custom_checkout_field_as_order_meta( $order, $data ) {
$key_field = 'fbhoaczalo';
if( isset($_POST[$key_field]) && !empty($_POST[$key_field]) ) {
$order->update_meta_data($key_field, sanitize_textarea_field($_POST[$key_field]));
}
}
// Save custom checkout field value as user metadata
add_action( 'woocommerce_checkout_update_customer', 'save_custom_checkout_field_as_user_meta', 10, 2 );
function save_custom_checkout_field_as_user_meta( $customer, $data ) {
$key_field = 'fbhoaczalo';
if( isset($_POST['fbhoaczalo']) ) {
$customer->update_meta_data($key_field, sanitize_textarea_field($_POST[$key_field]));
}
}
// Display the custom field value to admin order and email notifications
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_order_custom_field_value', 10, 1 );
add_action( 'woocommerce_email_after_order_table', 'display_order_custom_field_value', 20, 1 );
function display_order_custom_field_value( $order ) {
if ( $fbhoaczalo = $order->get_meta('fbhoaczalo') ) {
printf('<p><strong>%s:</strong> %s</p>', __('Link Facebook/Zalo/Tele'), $fbhoaczalo);
}
}
现在它将与 HPOS 配合使用,无需额外的同步过程。