Automattic\WooCommerce\Internal\Admin

Homescreen::maybe_set_default_shipping_options_on_home()publicWC 1.0

Set free shipping in the same country as the store default Flag rate in all other countries when any of the following conditions are ture

  • The store sells physical products, has JP and WCS installed and connected, and is located in the US.
  • The store sells physical products, and is not located in US/Canada/Australia/UK (irrelevant if JP is installed or not).
  • The store sells physical products and is located in US, but JP and WCS are not installed.

Method of the class: Homescreen{}

No Hooks.

Return

Array.

Usage

$Homescreen = new Homescreen();
$Homescreen->maybe_set_default_shipping_options_on_home( $settings );
$settings(array) (required)
shared admin settings.

Homescreen::maybe_set_default_shipping_options_on_home() code WC 9.3.3

public function maybe_set_default_shipping_options_on_home( $settings ) {
	if ( ! function_exists( 'get_current_screen' ) ) {
		return $settings;
	}

	$current_screen = get_current_screen();

	// Abort if it's not the homescreen.
	if ( ! isset( $current_screen->id ) || 'woocommerce_page_wc-admin' !== $current_screen->id ) {
		return $settings;
	}

	// Abort if we already created the shipping options.
	$already_created = get_option( 'woocommerce_admin_created_default_shipping_zones' );
	if ( $already_created === 'yes' ) {
		return $settings;
	}

	$zone_count = count( \WC_Data_Store::load( 'shipping-zone' )->get_zones() );
	if ( $zone_count ) {
		update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' );
		update_option( 'woocommerce_admin_reviewed_default_shipping_zones', 'yes' );
		return $settings;
	}

	$user_skipped_obw           = $settings['onboarding']['profile']['skipped'] ?? false;
	$store_address              = $settings['preloadSettings']['general']['woocommerce_store_address'] ?? '';
	$product_types              = $settings['onboarding']['profile']['product_types'] ?? array();
	$user_has_set_store_country = $settings['onboarding']['profile']['is_store_country_set'] ?? false;

	// Do not proceed if user has not filled out their country in the onboarding profiler.
	if ( ! $user_has_set_store_country ) {
		return $settings;
	}

	// If user skipped the obw or has not completed the store_details
	// then we assume the user is going to sell physical products.
	if ( $user_skipped_obw || '' === $store_address ) {
		$product_types[] = 'physical';
	}

	if ( false === in_array( 'physical', $product_types, true ) ) {
		return $settings;
	}

	$country_code = wc_format_country_state_string( $settings['preloadSettings']['general']['woocommerce_default_country'] )['country'];
	$country_name = WC()->countries->get_countries()[ $country_code ] ?? null;

	$is_jetpack_installed = in_array( 'jetpack', $settings['plugins']['installedPlugins'] ?? array(), true );
	$is_wcs_installed     = in_array( 'woocommerce-services', $settings['plugins']['installedPlugins'] ?? array(), true );

	if (
		( 'US' === $country_code && $is_jetpack_installed )
		||
		( ! in_array( $country_code, array( 'CA', 'AU', 'NZ', 'SG', 'HK', 'GB', 'ES', 'IT', 'DE', 'FR', 'CL', 'AR', 'PE', 'BR', 'UY', 'GT', 'NL', 'AT', 'BE' ), true ) )
		||
		( 'US' === $country_code && false === $is_jetpack_installed && false === $is_wcs_installed )
	) {
		$zone = new \WC_Shipping_Zone();
		$zone->set_zone_name( $country_name );
		$zone->add_location( $country_code, 'country' );

		// Method creation has no default title, use the REST API to add a title.
		$instance_id = $zone->add_shipping_method( 'free_shipping' );
		$request     = new \WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
		$request->set_body_params(
			array(
				'settings' => array(
					'title' => 'Free shipping',
				),
			)
		);
		rest_do_request( $request );

		update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' );
		Shipping::delete_zone_count_transient();
	}

	return $settings;
}