//* Add gender field to the checkout page
function wps_add_select_checkout_field( $fields ) {
$fields['billing']['billing_gender'] = array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'label' => '性別',
'priority' => 7,
'options' => array(
'' => __( '性別を選択してください。', 'wps' ),
'男' => __( '男', 'wps' ),
'女' => __( '女', 'wps' )
),
'required' => true,
);
$fields['order']['order_comments'] = array(
'placeholder' => __( '学校名を入力してください。', 'wps' ),
'label' => '学校名'
);
return $fields;
}
add_action('woocommerce_checkout_fields', 'wps_add_select_checkout_field');
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['billing_gender'] ) ) {
update_post_meta( $order_id, 'gender', sanitize_text_field( $_POST['billing_gender'] ) );
}
if ( ! empty( $_POST['order_comments'] ) ) {
update_post_meta( $order_id, 'school', sanitize_text_field( $_POST['order_comments'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p class="form-field _gender_id_field"><strong>性別:</strong> ' . get_post_meta( $order->id, 'gender', true ) . '</p>';
echo '<p class="form-field _school_id_field"><strong>学校名:</strong> ' . get_post_meta( $order->id, 'school', true ) . '</p>';
}
转载于:https://www.jianshu.com/p/1e0cbdd3a32a