Automattic\WooCommerce\Database\Migrations
MigrationHelper::migrate_country_states_for_shipping_locations()
Migrate state codes in the shipping locations table.
Method of the class: MigrationHelper{}
No Hooks.
Return
null
. Nothing (null).
Usage
$result = MigrationHelper::migrate_country_states_for_shipping_locations( $country_code, $old_to_new_states_mapping ): void;
- $country_code(string) (required)
- The country that has the states for which the migration is needed.
- $old_to_new_states_mapping(array) (required)
- An associative array where keys are the old state codes and values are the new state codes.
MigrationHelper::migrate_country_states_for_shipping_locations() MigrationHelper::migrate country states for shipping locations code WC 9.7.1
private static function migrate_country_states_for_shipping_locations( string $country_code, array $old_to_new_states_mapping ): void { global $wpdb; // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared $sql = "SELECT location_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_code LIKE '{$country_code}:%'"; $locations_data = $wpdb->get_results( $sql, ARRAY_A ); foreach ( $locations_data as $location_data ) { $old_state_code = substr( $location_data['location_code'], 3 ); if ( array_key_exists( $old_state_code, $old_to_new_states_mapping ) ) { $new_location_code = "{$country_code}:{$old_to_new_states_mapping[$old_state_code]}"; $update_query = $wpdb->prepare( "UPDATE {$wpdb->prefix}woocommerce_shipping_zone_locations SET location_code=%s WHERE location_id=%d", $new_location_code, $location_data['location_id'] ); $wpdb->query( $update_query ); } } // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared }