I want to make woocomerce field required

Viewed 34

I want to gender filed required in the woocommerce. I placed the field. It creates a gender field but I was unable to create gender field required. Please, suggest me any correction.

add_action( 'woocommerce_after_checkout_billing_form','my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
 echo '<div id="my_custom_checkout_field">
 ' . __('Gender') . '';

 woocommerce_form_field( 'city_custom', array(
      'required' => 'true',
       'clear'   =>'true',
     'type'          => 'select',
     'options'     => array(
         '' => __( 'Select city' ),
         'Kiribathgoda' => __('Male', 'woocommerce' ),
         'Wattala' => __('Female', 'woocommerce' )),
     'class'         => array('my-field-class form-row-wide'),
     
     ), $checkout->get_value( 'city_custom' ));

 echo '</div>';
}

// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
 if ( isset($_POST['city_custom']) && ! empty($_POST['city_custom']) ) {
     $order->update_meta_data( 'city', sanitize_text_field( $_POST['city_custom'] ) );
 } 
}

// Display the custom field value on admin order pages after billing adress:
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><strong>'.__('Gender').':</strong> ' . $order->get_meta('Gender') . '</p>'; 
}

// Display the custom field value on email notifications:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
 echo '<p><strong>'.__('Gender').':</strong> ' . $order->get_meta('gender') . '</p>';
}  ```
0 Answers
Related