如何向 Woocommerce 结帐添加额外字段并显示在编辑订单页面中
How to add extra field to Woo Commerce checkout and display in edit order page
我在我的 WooCommerce 结帐中额外添加了三个字段(CVR 编号、参考编号和 ønsket leveringsdato)。它们在结账时正确显示,但一旦下订单,信息就不会发送到 WooCommerce backend/order 概览。我如何发送信息以便我们接收?
function add_woocommerce_billing_fields($billing_fields){
//reorder woo my billing address form fields
$billing_fields2['billing_first_name'] = $billing_fields['billing_first_name'];
$billing_fields2['billing_last_name'] = $billing_fields['billing_last_name'];
$billing_fields2['billing_vat'] = array(
'type' => 'text',
'label' => __('CVR nummer', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => true,
'clear' => true
);
$billing_fields2['billing_ref'] = array(
'type' => 'text',
'label' => __('Reference nummer', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$billing_fields2['billing_date'] = array(
'type' => 'text',
'label' => __('Ønsket leveringsdato', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$merged_billing_fields = $billing_fields2 + $billing_fields;
return $merged_billing_fields;
}
您可以使用 woocommerce_checkout_update_order_meta
和 woocommerce_admin_order_data_after_billing_address
动作挂钩。代码进入您的原生主题 functions.php 文件。
Using woocommerce_checkout_update_order_meta
hook you can
save value meta.
function custom_checkout_field_update_order_meta( $order_id ) {
if ( $_POST['billing_vat'] ){
update_post_meta( $order_id, 'billing_vat', esc_attr($_POST['billing_vat']) );
}
if ( $_POST['billing_ref'] ){
update_post_meta( $order_id, 'billing_ref', esc_attr($_POST['billing_ref']) );
}
if ( $_POST['billing_date'] ){
update_post_meta( $order_id, 'billing_date', esc_attr($_POST['billing_date']) );
}
}
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
Using woocommerce_admin_order_data_after_billing_address
hook
you can display meta value after billing address.
function custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('CVR nummer').':</strong> ' . get_post_meta( $order->get_id(), 'billing_vat', true ) . '</p>';
echo '<p><strong>'.__('Reference nummer').':</strong> ' . get_post_meta( $order->get_id(), 'billing_ref', true ) . '</p>';
echo '<p><strong>'.__('Ønsket leveringsdato').':</strong> ' . get_post_meta( $order->get_id(), 'billing_date', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
我在我的 WooCommerce 结帐中额外添加了三个字段(CVR 编号、参考编号和 ønsket leveringsdato)。它们在结账时正确显示,但一旦下订单,信息就不会发送到 WooCommerce backend/order 概览。我如何发送信息以便我们接收?
function add_woocommerce_billing_fields($billing_fields){
//reorder woo my billing address form fields
$billing_fields2['billing_first_name'] = $billing_fields['billing_first_name'];
$billing_fields2['billing_last_name'] = $billing_fields['billing_last_name'];
$billing_fields2['billing_vat'] = array(
'type' => 'text',
'label' => __('CVR nummer', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => true,
'clear' => true
);
$billing_fields2['billing_ref'] = array(
'type' => 'text',
'label' => __('Reference nummer', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$billing_fields2['billing_date'] = array(
'type' => 'text',
'label' => __('Ønsket leveringsdato', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$merged_billing_fields = $billing_fields2 + $billing_fields;
return $merged_billing_fields;
}
您可以使用 woocommerce_checkout_update_order_meta
和 woocommerce_admin_order_data_after_billing_address
动作挂钩。代码进入您的原生主题 functions.php 文件。
Using
woocommerce_checkout_update_order_meta
hook you can save value meta.
function custom_checkout_field_update_order_meta( $order_id ) {
if ( $_POST['billing_vat'] ){
update_post_meta( $order_id, 'billing_vat', esc_attr($_POST['billing_vat']) );
}
if ( $_POST['billing_ref'] ){
update_post_meta( $order_id, 'billing_ref', esc_attr($_POST['billing_ref']) );
}
if ( $_POST['billing_date'] ){
update_post_meta( $order_id, 'billing_date', esc_attr($_POST['billing_date']) );
}
}
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
Using
woocommerce_admin_order_data_after_billing_address
hook you can display meta value after billing address.
function custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('CVR nummer').':</strong> ' . get_post_meta( $order->get_id(), 'billing_vat', true ) . '</p>';
echo '<p><strong>'.__('Reference nummer').':</strong> ' . get_post_meta( $order->get_id(), 'billing_ref', true ) . '</p>';
echo '<p><strong>'.__('Ønsket leveringsdato').':</strong> ' . get_post_meta( $order->get_id(), 'billing_date', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );