0

down to the city checkout field on Woocommerce using the following code:

function change_city_to_dropdown( $fields ) {
 include('citynames.php');
 
 $city_args = wp_parse_args( 'citydelivery' , array(
  'type' => 'select',
  'options' => array_combine( $cities, $cities),
  'input_class' => array(
   'wc-enhanced-select',
  )
 ), $fields['shipping']['shipping_city'] );
 $fields['shipping']['shipping_city'] = $city_args;
 $fields['billing']['billing_city'] = $city_args;
 wc_enqueue_js( "
  jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
   var select2_args = { minimumResultsForSearch: 5 };
   jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
  });"
 );
 return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'change_city_to_dropdown' );

This does the job perfectly but I am unable to put a restriction on it. For eg the default value is "Select your city" but sometimes the customer doesn't select their city and order gets placed. I tried the following code but it didn't work:

 add_action('woocommerce_checkout_process', 'checkout_field_city_restriction()');
 function checkout_field_city_restriction() {
    global $woocommerce;
    // Check if set, if its not set add an error.
    if ($_POST[$city_args] == "Select your city")
     wc_add_notice( '<strong>Please select your city</strong>', 'error' );
 }

Can anyone tell me what am I doing wrong? Also, this file ('citynames.php') has the following code:

<?php

$cities = array(

'Select your city',
'Tall',
'Tando Jam',
'Topi',
'Pasni',
'Mastung',
'Kalat',
'Kamalia',
'Haroonabad',
'Ranipur',
 );
?>
UzairSalman
  • 1
  • 1
  • 4

1 Answers1

0

I've solved my problem.

This is the following that had to be done:

 add_action('woocommerce_checkout_process', 'checkout_field_city_restriction()');
 function checkout_field_city_restriction {
 global $woocommerce;
 // Check if set, if its not set add an error.
 if ($_POST[$billing_city] == "Select your city")
 wc_add_notice( '<strong>Please select your city</strong>', 'error' );
 }
UzairSalman
  • 1
  • 1
  • 4