From 52f257505460a65924e58abb5bf5b5f12a484ada Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 5 Jan 2016 15:06:45 +0000 Subject: [PATCH] Upgrade script --- includes/updates/woocommerce-update-2.6.php | 69 ++++++++++++++++----- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/includes/updates/woocommerce-update-2.6.php b/includes/updates/woocommerce-update-2.6.php index 429b5fc34dc..c0213604f07 100644 --- a/includes/updates/woocommerce-update-2.6.php +++ b/includes/updates/woocommerce-update-2.6.php @@ -16,7 +16,6 @@ global $wpdb; /** * Old (table rate) shipping zones to new core shipping zones migration. - * * zone_enabled and zone_type are no longer used, but it's safe to leave them be. */ if ( $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_shipping_zones` LIKE 'zone_enabled';" ) ) { @@ -25,21 +24,63 @@ if ( $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_shipping_zon } /** - * Core uses woocommerce_shipping_zone_methods instead of woocommerce_shipping_zone_shipping_methods. Migrate the data. + * Shipping zones in WC 2.6.0 use a table named woocommerce_shipping_zone_methods. + * Migrate the old data out of woocommerce_shipping_zone_shipping_methods into the new table and port over any known options (used by table rates and flat rate boxes). */ if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_shipping_zone_shipping_methods';" ) ) { - $old_methods = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" ); - if ( $old_methods ) { - foreach ( $old_methods as $old_method ) { - $wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( - 'zone_id' => $old_method->zone_id, - 'method_id' => $old_method->shipping_method_type, - 'method_order' => $old_method->shipping_method_order - ) ); - $old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings'; - add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $wpdb->insert_id . '_settings', get_option( $old_settings_key ) ); - } - } + $old_methods = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" ); + if ( $old_methods ) { + $max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ); + $max_old_id = $wpdb->get_var( "SELECT MAX(shipping_method_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods" ); + + // Avoid ID conflicts + $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods AUTO_INCREMENT = %d;", max( $max_new_id, $max_old_id ) + 1 ) ); + + // Move data + foreach ( $old_methods as $old_method ) { + $wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( + 'zone_id' => $old_method->zone_id, + 'method_id' => $old_method->shipping_method_type, + 'method_order' => $old_method->shipping_method_order + ) ); + + $new_instance_id = $wpdb->insert_id; + + // Move main settings + $older_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings'; + $old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '_' . $old_method->shipping_method_id . '_settings'; + add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $new_instance_id . '_settings', get_option( $old_settings_key, get_option( $older_settings_key ) ) ); + + if ( 'table_rate' === $old_method->shipping_method_type ) { + // Move priority settings + add_option( 'woocommerce_table_rate_default_priority_' . $new_instance_id, get_option( 'woocommerce_table_rate_default_priority_' . $old_method->shipping_method_id ) ); + add_option( 'woocommerce_table_rate_priorities_' . $new_instance_id, get_option( 'woocommerce_table_rate_priorities_' . $old_method->shipping_method_id ) ); + + // Move rates + $wpdb->update( + $wpdb->prefix . 'woocommerce_shipping_table_rates', + array( + 'shipping_method_id' => $new_instance_id + ), + array( + 'shipping_method_id' => $old_method->shipping_method_id + ) + ); + } + + if ( 'flat_rate_boxes' === $old_method->shipping_method_type ) { + $wpdb->update( + $wpdb->prefix . 'woocommerce_shipping_flat_rate_boxes', + array( + 'shipping_method_id' => $new_instance_id + ), + array( + 'shipping_method_id' => $old_method->shipping_method_id + ) + ); + } + } + } } /**