我在Woocommerce结账页面中添加了“VAT编号”额外字段,为了存储元数据,我在函数中添加了以下代码。php:
/* Save VAT Number in the order meta */
add_action( \'woocommerce_checkout_update_user_meta\', \'wda_woocommerce_checkout_update_user_meta\', 10, 2 );
function wda_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
if (isset($posted[\'vat_number\'])) {
$dob = sanitize_text_field( $posted[\'vat_number\'] );
update_user_meta( $customer_id, \'vat_number\', $dob);
}
}
为了在订单编辑屏幕中显示/编辑增值税编号,我还放置了以下内容:
/* Display/Edit VAT Number in order edit screen */
function wda_display_order_data_in_admin( $order ){ ?>
<div class="address">
<?php
echo \'<p><strong>\' . __( \'VAT n.\' ) . \':</strong>\' . $order->get_meta( \'_vat_number\' ) . \'</p>\'; ?>
</div>
<div class="edit_address">
<?php woocommerce_wp_text_input( array( \'id\' => \'_vat_number\', \'label\' => __( \'VAT n.\' ), \'wrapper_class\' => \'_billing_company_field\' ) ); ?>
</div>
<?php }
add_action( \'woocommerce_admin_order_data_after_billing_address\', \'wda_display_order_data_in_admin\' );
function wda_save_extra_details( $order_id, $post ){
$order = wc_get_order( $order_id );
$order->update_meta_data( \'_vat_number\', wc_clean( $_POST[ \'_vat_number\' ] ) );
//$order->update_meta_data( \'_another_field\', wc_clean( $_POST[ \'_another_field\' ] ) );
$order->save_meta_data();
}
add_action( \'woocommerce_process_shop_order_meta\', \'wda_save_extra_details\', 45, 2 );
问题是VAT编号未显示在订单页/编辑上。可能有什么问题?