WooCommerce更新后以色列地区配送方式与运费配置失效问题
Hey there! It looks like your old woocommerce_states code stopped working after a WooCommerce update—let's get this sorted so you can set up region-specific shipping (like fixed rates for Jerusalem and local pickup for Tiberias) again.
Step 1: Fix the Israel States List Code
First, let's correct your original code. I noticed there were duplicate keys (like ASHDOD appearing twice) which could cause conflicts, plus some formatting issues with special characters that might break compatibility with newer WooCommerce versions. Here's the cleaned-up, updated version:
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } add_filter( 'woocommerce_states', 'custom_israel_states_list' ); function custom_israel_states_list( $states ) { $states['IL'] = array( "ACRE" => "Acre", "AFULA" => "Afula", "ARAD" => "Arad", "ARRABA" => "Arraba", // Renamed to avoid duplicate ASHDOD key "ASHDOD" => "Ashdod", "ASHKELON" => "Ashkelon", "BAQA_AL_GHARBIYYE" => "Baqa al-Gharbiyye", "BAT_YAM" => "Bat Yam", "BEERSHEBA" => "Beersheba", "BEIT_SHEAN" => "Beit She'an", "BEIT_SHEMESH" => "Beit Shemesh", "BNEI_BRAK" => "Bnei Brak", "DIMONA" => "Dimona", "EILAT" => "Eilat", "EL_AD" => "El ad", "GIVAT_SHMUEL" => "Giv'at Shmuel", "GIVATAYIM" => "Givatayim", "HADERA" => "Hadera", "HAIFA" => "Haifa", "HERZLIYA" => "Herzliya", "HOD_HASHARON" => "Hod HaSharon", "HOLON" => "Holon", "JERUSALEM" => "Jerusalem", "KAFR_QASIM" => "Kafr Qasim", "KARMIEL" => "Karmiel", "KFAR_SABA" => "Kfar Saba", "KFAR_YONA" => "Kfar Yona", "KIRYAT_ATA" => "Kiryat Ata", "KIRYAT_BIALIK" => "Kiryat Bialik", "KIRYAT_GAT" => "Kiryat Gat", "KIRYAT_MALAKHI" => "Kiryat Malakhi", "KIRYAT_MOTZKIN" => "Kiryat Motzkin", "KIRYAT_ONO" => "Kiryat Ono", "KIRYAT_SHMONA" => "Kiryat Shmona", "KIRYAT_YAM" => "Kiryat Yam", "LOD" => "Lod", "MA_ALOT_TARSHIHA" => "Ma'alot-Tarshiha", "MIGDAL_HAEMEK" => "Migdal HaEmek", "MODIIN_MACCABIM_REUT" => "Modi'in-Maccabim-Reut", "NAHARIYA" => "Nahariya", "NAZARETH" => "Nazareth", "NESHER" => "Nesher", "NESS_ZIONA" => "Ness Ziona", "NETANYA" => "Netanya", "NETIVOT" => "Netivot", "NOF_HAGALIL" => "Nof HaGalil", "OFAKIM" => "Ofakim", "OR_AKIVA" => "Or Akiva", "OR_YEHUDA" => "Or Yehuda", "PETAH_TIKVA" => "Petah Tikva", "QALANSAWE" => "Qalansawe", "RA_ANANA" => "Ra'anana", "RAHAT" => "Rahat", "RAMAT_GAN" => "Ramat Gan", "RAMAT_HASHARON" => "Ramat HaSharon", "RAMLA" => "Ramla", "REHOVOT" => "Rehovot", "RISHON_LEZION" => "Rishon LeZion", "ROSH_HAAYIN" => "Rosh HaAyin", "SAFED" => "Safed", "SAKHNIN" => "Sakhnin", "SDEROT" => "Sderot", "SHEFA_AMR" => "Shefa-Amr", "TAMRA" => "Tamra", "TEL_AVIV" => "Tel Aviv", "TIBERIAS" => "Tiberias", "TIRA" => "Tira", "TIRAT_CARMEL" => "Tirat Carmel", "UMM_AL_FAHM" => "Umm al-Fahm", "YAVNE" => "Yavne", "YEHUD_MONOSSON" => "Yehud-Monosson", "JERUSALEM_DISTRICT" => "Jerusalem District", "NORTHERN_DISTRICT" => "Northern District", "HAIFA_DISTRICT" => "Haifa District", "CENTRAL_DISTRICT" => "Central District", "TEL_AVIV_DISTRICT" => "Tel Aviv District", "SOUTHERN_DISTRICT" => "Southern District", "JUDEA_SAMARIA" => "Judea and Samaria Area" ); return $states; }
Key fixes here:
- Removed duplicate
ASHDODentry (renamed the Arraba entry toARRABAto avoid key conflicts) - Replaced spaces in keys with underscores for better compatibility (WooCommerce handles alphanumeric/underscore keys more reliably)
- Fixed minor formatting issues with apostrophes and hyphens
Step 2: Set Up Region-Specific Shipping Rules
Now that your states list is working, let's configure the shipping options you need:
Option 1: Fixed Shipping Rate for Jerusalem
- Go to WooCommerce > Settings > Shipping
- Create a new Flat Rate shipping method (or edit an existing one)
- Under Zone Locations, select Israel > Jerusalem
- Set your fixed rate amount in the Cost field (e.g.,
15for 15 ILS)
Option 2: Show Local Pickup Only for Tiberias
- In the same Shipping settings, create a Local Pickup method
- Set its Zone Location to Israel > Tiberias
- If you don't want other shipping methods to appear for Tiberias, ensure those methods don't include Tiberias in their assigned zones
Advanced: Use Code to Dynamically Adjust Shipping Options
If you need more granular control (like hiding all other methods when Tiberias is selected), add this code snippet:
add_filter( 'woocommerce_package_rates', 'adjust_shipping_for_israel_regions', 10, 2 ); function adjust_shipping_for_israel_regions( $rates, $package ) { // Get the customer's selected region $selected_region = $package['destination']['state']; // Hide all shipping methods except Local Pickup for Tiberias if ( $selected_region === 'TIBERIAS' ) { foreach ( $rates as $rate_id => $rate ) { if ( $rate->method_id !== 'local_pickup' ) { unset( $rates[$rate_id] ); } } } // Add fixed surcharge for Jerusalem (if you prefer this over zone-based flat rates) if ( $selected_region === 'JERUSALEM' ) { foreach ( $rates as $rate_id => $rate ) { $rates[$rate_id]->cost += 15; // Add 15 ILS fixed fee } } return $rates; }
Step 3: Test the Setup
- Add a product to your cart
- Proceed to checkout
- Select Israel as the country, then choose Jerusalem or Tiberias as the region
- Verify that the correct shipping options/rates appear
Don't forget to clear your WooCommerce cache (WooCommerce > Status > Tools > Clear transients) after adding the code—shipping rates are cached by default, so this ensures your changes take effect immediately.
内容的提问来源于stack exchange,提问作者Victor S.

