woocommerce 结帐自定义字段更新订单元
woocommerce checkout custom fields update order meta
基于@LoicTheAztec 的 ,我能够创建统一运费方式的自定义字段,我可以在 MySQL 上找到数据,但它没有显示在订单上详细信息,电子邮件。
如何将此添加到订单详细信息和电子邮件中?这是我基于 post
使用的代码
// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
if( ! is_checkout()) return; // Only on checkout page
$customer_carrier_method = 'flat_rate:4';
if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"
$chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];
// If the chosen shipping method is 'legacy_local_pickup' we display
if($chosen_method_id == $customer_carrier_method ):
echo '<div class="custom-carrier">';
woocommerce_form_field( 'ds_name' , array(
'type' => 'text',
'class' => array('form-row-wide ds-name'),
'required' => true,
'placeholder' => 'Nama Toko',
), WC()->checkout->get_value( 'ds_name' ));
woocommerce_form_field( 'ds_number' , array(
'type' => 'text',
'class' => array('form-row-wide ds-number'),
'required' => true,
'placeholder' => 'Kode Booking',
), WC()->checkout->get_value( 'ds_number' ));
echo '</div>';
endif;
}
// Check custom fields validation
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
if( isset( $_POST['ds_name'] ) && empty( $_POST['ds_name'] ) )
wc_add_notice( ( "Anda belum memasukkan Nama Toko" ), "error" );
if( isset( $_POST['ds_number'] ) && empty( $_POST['ds_number'] ) )
wc_add_notice( ( "Anda belum memasukkan nomor kode booking" ), "error" );
}
// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['ds_name'] ))
update_post_meta( $order_id, '_ds_name', sanitize_text_field( $_POST['ds_name'] ) );
if( isset( $_POST['ds_number'] ))
update_post_meta( $order_id, '_ds_number', sanitize_text_field( $_POST['ds_number'] ) );
}
找到解决方案,删除 update_post_meta 上的 _(下划线),然后在订单编辑中显示。
// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['ds_name'] ))
update_post_meta( $order_id, 'ds_name', sanitize_text_field( $_POST['ds_name'] ) );
if( isset( $_POST['ds_number'] ))
update_post_meta( $order_id, 'ds_number', sanitize_text_field( $_POST['ds_number'] ) );
}
基于@LoicTheAztec 的
// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
if( ! is_checkout()) return; // Only on checkout page
$customer_carrier_method = 'flat_rate:4';
if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"
$chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];
// If the chosen shipping method is 'legacy_local_pickup' we display
if($chosen_method_id == $customer_carrier_method ):
echo '<div class="custom-carrier">';
woocommerce_form_field( 'ds_name' , array(
'type' => 'text',
'class' => array('form-row-wide ds-name'),
'required' => true,
'placeholder' => 'Nama Toko',
), WC()->checkout->get_value( 'ds_name' ));
woocommerce_form_field( 'ds_number' , array(
'type' => 'text',
'class' => array('form-row-wide ds-number'),
'required' => true,
'placeholder' => 'Kode Booking',
), WC()->checkout->get_value( 'ds_number' ));
echo '</div>';
endif;
}
// Check custom fields validation
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
if( isset( $_POST['ds_name'] ) && empty( $_POST['ds_name'] ) )
wc_add_notice( ( "Anda belum memasukkan Nama Toko" ), "error" );
if( isset( $_POST['ds_number'] ) && empty( $_POST['ds_number'] ) )
wc_add_notice( ( "Anda belum memasukkan nomor kode booking" ), "error" );
}
// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['ds_name'] ))
update_post_meta( $order_id, '_ds_name', sanitize_text_field( $_POST['ds_name'] ) );
if( isset( $_POST['ds_number'] ))
update_post_meta( $order_id, '_ds_number', sanitize_text_field( $_POST['ds_number'] ) );
}
找到解决方案,删除 update_post_meta 上的 _(下划线),然后在订单编辑中显示。
// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['ds_name'] ))
update_post_meta( $order_id, 'ds_name', sanitize_text_field( $_POST['ds_name'] ) );
if( isset( $_POST['ds_number'] ))
update_post_meta( $order_id, 'ds_number', sanitize_text_field( $_POST['ds_number'] ) );
}