限制 woocommerce 结帐时的城市下拉列表

Restriction on city dropdown on checkout of woocommerce

使用以下代码进入 Woocommerce 上的城市结帐字段:

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' );

这可以完美地完成工作,但我无法对其施加限制。例如,默认值是 "Select your city" 但有时客户不会 select 他们的城市和订单被放置。我尝试了以下代码,但没有用:

 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' );
 }

谁能告诉我我做错了什么? 此外,此文件 ('citynames.php') 具有以下代码:

<?php

$cities = array(

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

我的问题已经解决了。

这是必须完成的以下操作:

 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' );
 }