wc_update_240_shipping_methods()
Update shipping methods for 2.4
Description Description
Return Return
(void)
Source Source
File: includes/wc-update-functions.php
function wc_update_240_shipping_methods() { /** * Flat Rate Shipping. * Update legacy options to new math based options. */ $shipping_methods = array( 'woocommerce_flat_rates' => new WC_Shipping_Legacy_Flat_Rate(), 'woocommerce_international_delivery_flat_rates' => new WC_Shipping_Legacy_International_Delivery(), ); foreach ( $shipping_methods as $flat_rate_option_key => $shipping_method ) { // Stop this running more than once if routine is repeated. if ( version_compare( $shipping_method->get_option( 'version', 0 ), '2.4.0', '<' ) ) { $shipping_classes = WC()->shipping()->get_shipping_classes(); $has_classes = count( $shipping_classes ) > 0; $cost_key = $has_classes ? 'no_class_cost' : 'cost'; $min_fee = $shipping_method->get_option( 'minimum_fee' ); $math_cost_strings = array( 'cost' => array(), 'no_class_cost' => array(), ); $math_cost_strings[ $cost_key ][] = $shipping_method->get_option( 'cost' ); $fee = $shipping_method->get_option( 'fee' ); if ( $fee ) { $math_cost_strings[ $cost_key ][] = strstr( $fee, '%' ) ? '[fee percent="' . str_replace( '%', '', $fee ) . '" min="' . esc_attr( $min_fee ) . '"]' : $fee; } foreach ( $shipping_classes as $shipping_class ) { $rate_key = 'class_cost_' . $shipping_class->slug; $math_cost_strings[ $rate_key ] = $math_cost_strings['no_class_cost']; } $flat_rates = array_filter( (array) get_option( $flat_rate_option_key, array() ) ); if ( $flat_rates ) { foreach ( $flat_rates as $shipping_class => $rate ) { $rate_key = 'class_cost_' . $shipping_class; if ( $rate['cost'] || $rate['fee'] ) { $math_cost_strings[ $rate_key ][] = $rate['cost']; $math_cost_strings[ $rate_key ][] = strstr( $rate['fee'], '%' ) ? '[fee percent="' . str_replace( '%', '', $rate['fee'] ) . '" min="' . esc_attr( $min_fee ) . '"]' : $rate['fee']; } } } if ( 'item' === $shipping_method->type ) { foreach ( $math_cost_strings as $key => $math_cost_string ) { $math_cost_strings[ $key ] = array_filter( array_map( 'trim', $math_cost_strings[ $key ] ) ); if ( ! empty( $math_cost_strings[ $key ] ) ) { $last_key = max( 0, count( $math_cost_strings[ $key ] ) - 1 ); $math_cost_strings[ $key ][0] = '( ' . $math_cost_strings[ $key ][0]; $math_cost_strings[ $key ][ $last_key ] .= ' ) * [qty]'; } } } $math_cost_strings['cost'][] = $shipping_method->get_option( 'cost_per_order' ); // Save settings. foreach ( $math_cost_strings as $option_id => $math_cost_string ) { $shipping_method->settings[ $option_id ] = implode( ' + ', array_filter( $math_cost_string ) ); } $shipping_method->settings['version'] = '2.4.0'; $shipping_method->settings['type'] = 'item' === $shipping_method->settings['type'] ? 'class' : $shipping_method->settings['type']; update_option( $shipping_method->plugin_id . $shipping_method->id . '_settings', $shipping_method->settings ); } } }