Upgrade script

This commit is contained in:
Mike Jolley 2016-01-05 15:06:45 +00:00
parent 1630913c62
commit 52f2575054
1 changed files with 55 additions and 14 deletions

View File

@ -16,7 +16,6 @@ global $wpdb;
/** /**
* Old (table rate) shipping zones to new core shipping zones migration. * 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. * 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';" ) ) { 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';" ) ) { 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;" ); $old_methods = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" );
if ( $old_methods ) { if ( $old_methods ) {
foreach ( $old_methods as $old_method ) { $max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" );
$wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( $max_old_id = $wpdb->get_var( "SELECT MAX(shipping_method_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods" );
'zone_id' => $old_method->zone_id,
'method_id' => $old_method->shipping_method_type, // Avoid ID conflicts
'method_order' => $old_method->shipping_method_order $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods AUTO_INCREMENT = %d;", max( $max_new_id, $max_old_id ) + 1 ) );
) );
$old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings'; // Move data
add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $wpdb->insert_id . '_settings', get_option( $old_settings_key ) ); 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
)
);
}
}
}
} }
/** /**