wc_update_260_zone_methods

The WooCommerce wc update 260 zone methods function.

Description

wc_update_260_zone_methods(); 

Usage

  1. if ( !function_exists( 'wc_update_260_zone_methods' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-update-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wc_update_260_zone_methods(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-update-functions.php  
  1. function wc_update_260_zone_methods() { 
  2. global $wpdb; 
  3.  
  4. /** 
  5. * Shipping zones in WC 2.6.0 use a table named woocommerce_shipping_zone_methods. 
  6. * 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). 
  7. */ 
  8. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_shipping_zone_shipping_methods';" ) ) { 
  9. $old_methods = $wpdb->get_results( "SELECT zone_id, shipping_method_type, shipping_method_order, shipping_method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" ); 
  10.  
  11. if ( $old_methods ) { 
  12. $max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ); 
  13. $max_old_id = $wpdb->get_var( "SELECT MAX(shipping_method_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods" ); 
  14.  
  15. // Avoid ID conflicts 
  16. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods AUTO_INCREMENT = %d;", max( $max_new_id, $max_old_id ) + 1 ) ); 
  17.  
  18. // Store changes 
  19. $changes = array(); 
  20.  
  21. // Move data 
  22. foreach ( $old_methods as $old_method ) { 
  23. $wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 
  24. 'zone_id' => $old_method->zone_id,  
  25. 'method_id' => $old_method->shipping_method_type,  
  26. 'method_order' => $old_method->shipping_method_order,  
  27. ) ); 
  28.  
  29. $new_instance_id = $wpdb->insert_id; 
  30.  
  31. // Move main settings 
  32. $older_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings'; 
  33. $old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '_' . $old_method->shipping_method_id . '_settings'; 
  34. add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $new_instance_id . '_settings', get_option( $old_settings_key, get_option( $older_settings_key ) ) ); 
  35.  
  36. // Handling for table rate and flat rate box shipping. 
  37. if ( 'table_rate' === $old_method->shipping_method_type ) { 
  38. // Move priority settings 
  39. add_option( 'woocommerce_table_rate_default_priority_' . $new_instance_id, get_option( 'woocommerce_table_rate_default_priority_' . $old_method->shipping_method_id ) ); 
  40. add_option( 'woocommerce_table_rate_priorities_' . $new_instance_id, get_option( 'woocommerce_table_rate_priorities_' . $old_method->shipping_method_id ) ); 
  41.  
  42. // Move rates 
  43. $wpdb->update( 
  44. $wpdb->prefix . 'woocommerce_shipping_table_rates',  
  45. array( 
  46. 'shipping_method_id' => $new_instance_id,  
  47. ),  
  48. array( 
  49. 'shipping_method_id' => $old_method->shipping_method_id,  
  50. ); 
  51. } elseif ( 'flat_rate_boxes' === $old_method->shipping_method_type ) { 
  52. $wpdb->update( 
  53. $wpdb->prefix . 'woocommerce_shipping_flat_rate_boxes',  
  54. array( 
  55. 'shipping_method_id' => $new_instance_id,  
  56. ),  
  57. array( 
  58. 'shipping_method_id' => $old_method->shipping_method_id,  
  59. ); 
  60.  
  61. $changes[ $old_method->shipping_method_id ] = $new_instance_id; 
  62.  
  63. // $changes contains keys (old method ids) and values (new instance ids) if extra processing is needed in plugins. 
  64. // Store this to an option so extensions can pick it up later, then fire an action. 
  65. update_option( 'woocommerce_updated_instance_ids', $changes ); 
  66. do_action( 'woocommerce_updated_instance_ids', $changes ); 
  67.  
  68. // Change ranges used to ... 
  69. $wpdb->query( "UPDATE {$wpdb->prefix}woocommerce_shipping_zone_locations SET location_code = REPLACE( location_code, '-', '...' );" );