问题描述
// Hook in
add_filter( 'woocommerce_checkout_fields','custom_override_checkout_fields' );
add_filter( 'woocommerce_ship_to_different_address_checked','__return_false' );
add_action( 'woocommerce_before_order_notes','competitor_details' );
add_action('woocommerce_checkout_process','my_custom_checkout_field_process');
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}
function competitor_details( $checkout ) {
echo '<div id="competitor_details"><h3>' . __('Competitor\'s Details') . '</h3>';
woocommerce_form_field( 'competitor_first_name',array('label' => 'First name','type' => 'text','required' => 'true'),$checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_last_name',array('label' => 'Last name',$checkout->get_value( 'my_field_name' ));
echo '<div id="competitor_details"><h4>' . __('Date of birth') . '</h4>';
woocommerce_form_field( 'competitor_dob_day',array('label' => 'Day','type' => 'select','required' => 'true','options' => array(''=>'','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10','11'=>'11','12'=>'12','13'=>'13','14'=>'14','15'=>'15','16'=>'16','17'=>'17','18'=>'18','19'=>'19','20'=>'20','21'=>'21','22'=>'22','23'=>'23','24'=>'24','25'=>'25','26'=>'26','27'=>'27','28'=>'28','29'=>'29','30'=>'30','31'=>'31')),$checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_dob_month',array('label' => 'Month','Jan'=>'Jan','Feb'=>'Feb','Mar'=>'Mar','Apr'=>'Apr','May'=>'May','Jun'=>'Jun','Jul'=>'Jul','Aug'=>'Aug','Sep'=>'Sep','Oct'=>'Oct','Nov'=>'Nov','Dec'=>'Dec')),$checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_dob_year',array('label' => 'Year',$checkout->get_value( 'my_field_name' ));
echo '<div id="consent_to_rules"><h4>' . __('I have read the Rules and agree to abide by them') . '</h4>';
woocommerce_form_field( 'consent_to_rules',array('label' => 'YES,I agree','type' => 'checkBox',$checkout->get_value( 'my_field_name' ));
echo '</div>';
}
function my_custom_checkout_field_process() {
// Check if set,if its not set add an error.
if ( ! $_POST['competitor_first_name'] )
wc_add_notice( __( 'Please enter Competitors First Name' ),'error' );
if ( ! $_POST['competitor_last_name'] )
wc_add_notice( __( 'Please enter Competitors Last Name' ),'error' );
if ( ! $_POST['competitor_dob_day'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ),'error' );
if ( ! $_POST['competitor_dob_month'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ),'error' );
if ( ! $_POST['competitor_dob_year'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ),'error' );
if ( ! $_POST['consent_to_rules'] )
wc_add_notice( __( 'Please agree that you have read the Rules' ),'error' );
}
这会将必填字段添加到结帐页面。我不想要日期选择器,因为 dob 可以是任何东西 - 它只是必须易于输入。
结帐工作正常,但在管理区域查看订单详细信息时看不到字段值。
有什么帮助吗?
解决方法
首先,我已经重写了您当前的代码,因为它有一些小错误
// Unset order comments field
function filter_woocommerce_checkout_fields( $fields ) {
unset( $fields['order']['order_comments'] );
return $fields;
}
add_filter( 'woocommerce_checkout_fields','filter_woocommerce_checkout_fields',10,1 );
// NOT checked by default
add_filter( 'woocommerce_ship_to_different_address_checked','__return_false' );
// Add custom fields
function action_woocommerce_before_order_notes( $checkout ) {
echo '<h3>' . __( 'Competitor\'s Details','woocommerce' ) . '</h3>';
// First name
woocommerce_form_field( 'competitor_first_name',array(
'label' => __( 'First name','woocommerce' ),'type' => 'text','required' => 'true'
),$checkout->get_value( 'competitor_first_name' ) );
// Last name
woocommerce_form_field( 'competitor_last_name',array(
'label' => __( 'Last name',$checkout->get_value( 'competitor_last_name' ) );
echo '<h4>' . __( 'Date of birth','woocommerce' ) . '</h4>';
// Options day
$options_day = array( '' => __( 'Please select a day','woocommerce' ) );
$from = 1;
$to = 31;
$options_day_combine = $options_day + array_combine( range( $from,$to ),range( $from,$to ) );
// Dob day
woocommerce_form_field( 'competitor_dob_day',array(
'label' => __( 'Day','type' => 'select','required' => 'true','options' => $options_day_combine
),$checkout->get_value( 'competitor_dob_day' ) );
// Dob month
woocommerce_form_field( 'competitor_dob_month',array(
'label' => __( 'Month','options' => array(
'' => __( 'Please select a month','Jan' => __( 'Jan','Feb' => __( 'Feb','Mar' => __( 'Mar','Apr' => __( 'Apr','May' => __( 'May','Jun' => __( 'Jun','Jul' => __( 'Jul','Aug' => __( 'Aug','Sep' => __( 'Sep','Oct' => __( 'Oct','Nov' => __( 'Nov','Dec' => __( 'Dec',)
),$checkout->get_value( 'competitor_dob_month' ) );
// Options year
$options_year = array( '' => __( 'Please select a year','woocommerce' ) );
$from = 2021;
$to = 1910;
$options_year_combine = $options_year + array_combine( range( $from,$to ) );
// Dob year
woocommerce_form_field( 'competitor_dob_year',array(
'label' => __( 'Year','options' => $options_year_combine
),$checkout->get_value( 'competitor_dob_year' ) );
echo '<h4>' . __( 'I have read the Rules and agree to abide by them','woocommerce' ) . '</h4>';
// Rules
woocommerce_form_field( 'consent_to_rules',array(
'label' => __( 'Yes,I agree','type' => 'checkbox',$checkout->get_value( 'consent_to_rules' ) );
}
add_action( 'woocommerce_before_order_notes','action_woocommerce_before_order_notes',1 );
// Validate custom fields
function action_woocommerce_checkout_process() {
// Isset and empty,add an error
if ( isset( $_POST['competitor_first_name'] ) && empty( $_POST['competitor_first_name'] ) ) {
wc_add_notice( __( 'Please enter Competitors First Name','error' );
}
if ( isset( $_POST['competitor_last_name'] ) && empty( $_POST['competitor_last_name'] ) ) {
wc_add_notice( __( 'Please enter Competitors Last Name','error' );
}
if ( isset( $_POST['competitor_dob_day'] ) && empty( $_POST['competitor_dob_day'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - day','error' );
}
if ( isset( $_POST['competitor_dob_month'] ) && empty( $_POST['competitor_dob_month'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - month','error' );
}
if ( isset( $_POST['competitor_dob_year'] ) && empty( $_POST['competitor_dob_year'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - year','error' );
}
if ( ! isset( $_POST['consent_to_rules'] ) ) {
wc_add_notice( __( 'Please agree that you have read the Rules','error' );
}
}
add_action( 'woocommerce_checkout_process','action_woocommerce_checkout_process',0 );
要回答您的问题,请保存并显示
使用 woocommerce_checkout_create_order
和 woocommerce_admin_order_data_after_billing_address
操作挂钩。
这部分可以用 foreach 循环进行优化,而不是每次都添加一个 if 条件,但我没有应用它,所以一切都保持清晰。
所以你得到:
// Save fields
function action_woocommerce_checkout_create_order( $order,$data ) {
// Isset and NOT empty,save
if ( isset( $_POST['competitor_first_name'] ) && ! empty( $_POST['competitor_first_name'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_first_name',sanitize_text_field( $_POST['competitor_first_name'] ) );
}
if ( isset( $_POST['competitor_last_name'] ) && ! empty( $_POST['competitor_last_name'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_last_name',sanitize_text_field( $_POST['competitor_last_name'] ) );
}
if ( isset( $_POST['competitor_dob_day'] ) && ! empty( $_POST['competitor_dob_day'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_day',sanitize_text_field( $_POST['competitor_dob_day'] ) );
}
if ( isset( $_POST['competitor_dob_month'] ) && ! empty( $_POST['competitor_dob_month'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_month',sanitize_text_field( $_POST['competitor_dob_month'] ) );
}
if ( isset( $_POST['competitor_dob_year'] ) && ! empty( $_POST['competitor_dob_year'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_year',sanitize_text_field( $_POST['competitor_dob_year'] ) );
}
if ( isset( $_POST['consent_to_rules'] ) ) {
// Update meta data
$order->update_meta_data( 'consent_to_rules',sanitize_text_field( $_POST['consent_to_rules'] ) );
}
}
add_action( 'woocommerce_checkout_create_order','action_woocommerce_checkout_create_order',2 );
// Display the custom 'select' field value on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Get meta
$competitor_first_name = $order->get_meta( 'competitor_first_name' );
// NOT empty
if ( ! empty ( $competitor_first_name ) ) {
echo '<p><strong>' . __( 'Competitor first name','woocommerce' ) . ':</strong> ' . $competitor_first_name . '</p>';
}
// Get meta
$competitor_last_name = $order->get_meta( 'competitor_last_name' );
// NOT empty
if ( ! empty ( $competitor_last_name ) ) {
echo '<p><strong>' . __( 'Competitor last name','woocommerce' ) . ':</strong> ' . $competitor_last_name . '</p>';
}
// Get meta
$competitor_dob_day = $order->get_meta( 'competitor_dob_day' );
// NOT empty
if ( ! empty ( $competitor_dob_day ) ) {
echo '<p><strong>' . __( 'Competitor dob day','woocommerce' ) . ':</strong> ' . $competitor_dob_day . '</p>';
}
// Get meta
$competitor_dob_month = $order->get_meta( 'competitor_dob_month' );
// NOT empty
if ( ! empty ( $competitor_dob_month ) ) {
echo '<p><strong>' . __( 'Competitor dob month','woocommerce' ) . ':</strong> ' . $competitor_dob_month . '</p>';
}
// Get meta
$competitor_dob_year = $order->get_meta( 'competitor_dob_year' );
// NOT empty
if ( ! empty ( $competitor_dob_year ) ) {
echo '<p><strong>' . __( 'Competitor dob year','woocommerce' ) . ':</strong> ' . $competitor_dob_year . '</p>';
}
// Get meta
$consent_to_rules = $order->get_meta( 'consent_to_rules' );
// NOT empty
if ( ! empty ( $consent_to_rules ) ) {
echo '<p><strong>' . __( 'Yes,'woocommerce' ) . '</strong></p>';
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address','action_woocommerce_admin_order_data_after_billing_address',1 );