wc_update_240_shipping_methods

The WooCommerce wc update 240 shipping methods function.

Description

wc_update_240_shipping_methods(); 

Usage

  1. if ( !function_exists( 'wc_update_240_shipping_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_240_shipping_methods(); 
  7.  

Defined (1)

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

/includes/wc-update-functions.php  
  1. function wc_update_240_shipping_methods() { 
  2. global $wpdb; 
  3. /** 
  4. * Flat Rate Shipping. 
  5. * Update legacy options to new math based options. 
  6. */ 
  7. $shipping_methods = array( 
  8. 'woocommerce_flat_rates' => new WC_Shipping_Legacy_Flat_Rate(),  
  9. 'woocommerce_international_delivery_flat_rates' => new WC_Shipping_Legacy_International_Delivery(),  
  10. ); 
  11. foreach ( $shipping_methods as $flat_rate_option_key => $shipping_method ) { 
  12. // Stop this running more than once if routine is repeated 
  13. if ( version_compare( $shipping_method->get_option( 'version', 0 ), '2.4.0', '<' ) ) { 
  14. $has_classes = sizeof( WC()->shipping->get_shipping_classes() ) > 0; 
  15. $cost_key = $has_classes ? 'no_class_cost' : 'cost'; 
  16. $min_fee = $shipping_method->get_option( 'minimum_fee' ); 
  17. $math_cost_strings = array( 'cost' => array(), 'no_class_cost' => array() ); 
  18. $math_cost_strings[ $cost_key ][] = $shipping_method->get_option( 'cost' ); 
  19.  
  20. if ( $fee = $shipping_method->get_option( 'fee' ) ) { 
  21. $math_cost_strings[ $cost_key ][] = strstr( $fee, '%' ) ? '[fee percent="' . str_replace( '%', '', $fee ) . '" min="' . esc_attr( $min_fee ) . '"]' : $fee; 
  22.  
  23. foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) { 
  24. $rate_key = 'class_cost_' . $shipping_class->slug; 
  25. $math_cost_strings[ $rate_key ] = $math_cost_strings['no_class_cost']; 
  26.  
  27. if ( $flat_rates = array_filter( (array) get_option( $flat_rate_option_key, array() ) ) ) { 
  28. foreach ( $flat_rates as $shipping_class => $rate ) { 
  29. $rate_key = 'class_cost_' . $shipping_class; 
  30. if ( $rate['cost'] || $rate['fee'] ) { 
  31. $math_cost_strings[ $rate_key ][] = $rate['cost']; 
  32. $math_cost_strings[ $rate_key ][] = strstr( $rate['fee'], '%' ) ? '[fee percent="' . str_replace( '%', '', $rate['fee'] ) . '" min="' . esc_attr( $min_fee ) . '"]' : $rate['fee']; 
  33.  
  34. if ( 'item' === $shipping_method->type ) { 
  35. foreach ( $math_cost_strings as $key => $math_cost_string ) { 
  36. $math_cost_strings[ $key ] = array_filter( array_map( 'trim', $math_cost_strings[ $key ] ) ); 
  37. if ( ! empty( $math_cost_strings[ $key ] ) ) { 
  38. $last_key = max( 0, sizeof( $math_cost_strings[ $key ] ) - 1 ); 
  39. $math_cost_strings[ $key ][0] = '( ' . $math_cost_strings[ $key ][0]; 
  40. $math_cost_strings[ $key ][ $last_key ] .= ' ) * [qty]'; 
  41.  
  42. $math_cost_strings['cost'][] = $shipping_method->get_option( 'cost_per_order' ); 
  43.  
  44. // Save settings 
  45. foreach ( $math_cost_strings as $option_id => $math_cost_string ) { 
  46. $shipping_method->settings[ $option_id ] = implode( ' + ', array_filter( $math_cost_string ) ); 
  47.  
  48. $shipping_method->settings['version'] = '2.4.0'; 
  49. $shipping_method->settings['type'] = 'item' === $shipping_method->settings['type'] ? 'class' : $shipping_method->settings['type']; 
  50.  
  51. update_option( $shipping_method->plugin_id . $shipping_method->id . '_settings', $shipping_method->settings );