acebook and Instagram are determined by post-discount subtotals. // Don't sync rate if using pre-discount amounts if ( 'yes' === $shipping_settings['ignore_discounts'] ) { self::log_map_shipping_method_issue_to_meta( $zone, $free_shipping_method, 'Min spend free shipping ignores discounts', 'map_free_shipping_method' ); return null; } $min_spend = $free_shipping_method['instance_settings']['min_amount'] ?? 0; $shipping_data['cart_minimum_for_free_shipping'] = $min_spend . ' ' . get_woocommerce_currency(); } return $shipping_data; } /** * Flat rate shipping can still be configured to be free for all or a subset of products based on shipping classes. * TODO - Currently syncs only if free for all products, need to extract free and non-free products based on shipping class. * * @param array $zone * @param array $flat_rate_method * @return array|null */ private static function get_flat_rate_shipping_method_data( array $zone, array $flat_rate_method ): ?array { $shipping_settings = $flat_rate_method['instance_settings']; // If the base cost isn't free we don't need to bother syncing this shipping method. if ( ! self::is_zero_cost( $shipping_settings['cost'] ?? '0' ) ) { self::log_map_shipping_method_issue_to_meta( $zone, $flat_rate_method, 'Flat rate shipping has base cost', 'map_flat_rate_shipping_method' ); return null; } // For each shipping class, a new key is inserted into the methods settings with form 'class_cost_{class_id}' // The value is the additional cost to ship for products of that class when using the shipping method. If // // some classes have a cost, data will be synced as a separate shipping profile for any class that has a 0 cost. $shipping_class_ids_to_costs = []; $class_cost_prefix = 'class_cost_'; $prefix_length = strlen( $class_cost_prefix ); foreach ( $shipping_settings as $key => $value ) { if ( 0 === strpos( $key, $class_cost_prefix ) ) { $shipping_class_id = substr( $key, $prefix_length ); $shipping_class_ids_to_costs[ $shipping_class_id ] = $value; } } $shipping_class_ids_to_costs[ self::NO_SHIPPING_CLASS_ID ] = $shipping_settings['no_class_cost'] ?? '0'; $free_shipping_class_ids = []; $paid_shipping_class_ids = []; foreach ( $shipping_class_ids_to_costs as $class_id => $cost ) { if ( self::is_zero_cost( $cost ) ) { $free_shipping_class_ids[] = $class_id; } else { $paid_shipping_class_ids[] = $class_id; } } if ( count( $free_shipping_class_ids ) === 0 ) { self::log_map_shipping_method_issue_to_meta( $zone, $flat_rate_method, 'Flat rate shipping has no free classes', 'map_flat_rate_shipping_method' ); return null; } $free_shipping_applies_to_all_products = empty( $paid_shipping_class_ids ); return array( 'name' => $flat_rate_method['title'], 'has_free_shipping' => 'true', 'applies_to_all_products' => $free_shipping_applies_to_all_products, 'shipping_class_ids' => $free_shipping_class_ids, ); } private static function log_map_shipping_method_issue_to_meta( array $zone, array $shipping_method, string $message, string $flow_step ): void { Logger::log( $message, array( 'flow_name' => FeedUploadUtils::SHIPPING_PROFILES_SYNC_LOGGING_FLOW_NAME, 'flow_step' => $flow_step, 'extra_data' => [ 'zone_id' => $zone['id'], 'zone_name' => $zone['zone_name'], 'method_id' => $shipping_method['instance_id'], ], ), array( 'should_send_log_to_meta' => true, 'should_save_log_in_woocommerce' => true, 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, ) ); } private static function add_continent_location( string $continent_code, array $countries_to_states ): array { $country_codes = WC()->countries->get_continents()[ $continent_code ]['countries']; foreach ( $country_codes as $country_code ) { $countries_to_states = self::add_country_location( $country_code, $countries_to_states ); } return $countries_to_states; } private static function add_country_location( string $country_code, array $countries_to_states ): array { $countries_to_states[ $country_code ]['applies_to_entire_country'] = true; return $countries_to_states; } private static function is_zero_cost( string $cost_string ): bool { if ( empty( $cost_string ) ) { return true; } if ( is_numeric( $cost_string ) ) { return 0.0 === (float) $cost_string; } return false; } public static function get_shipping_class_tag_for_class( string $class_id ): string { if ( self::NO_SHIPPING_CLASS_ID === $class_id ) { return self::NO_SHIPPING_CLASS_TAG; } return self::SHIPPING_CLASS_TAG_PREFIX . $class_id; } }