Warning: This method has been deprecated.

WC_Admin_Setup_Wizard::wc_setup_shipping_save()

Save shipping options.


Description Description


Source Source

File: includes/admin/class-wc-admin-setup-wizard.php

	public function wc_setup_shipping_save() {
		check_admin_referer( 'wc-setup' );

		// @codingStandardsIgnoreStart
		$setup_domestic   = isset( $_POST['shipping_zones']['domestic']['enabled'] ) && ( 'yes' === $_POST['shipping_zones']['domestic']['enabled'] );
		$domestic_method  = isset( $_POST['shipping_zones']['domestic']['method'] ) ? sanitize_text_field( wp_unslash( $_POST['shipping_zones']['domestic']['method'] ) ) : '';
		$setup_intl       = isset( $_POST['shipping_zones']['intl']['enabled'] ) && ( 'yes' === $_POST['shipping_zones']['intl']['enabled'] );
		$intl_method      = isset( $_POST['shipping_zones']['intl']['method'] ) ? sanitize_text_field( wp_unslash( $_POST['shipping_zones']['intl']['method'] ) ) : '';
		$weight_unit      = sanitize_text_field( wp_unslash( $_POST['weight_unit'] ) );
		$dimension_unit   = sanitize_text_field( wp_unslash( $_POST['dimension_unit'] ) );
		$existing_zones   = WC_Shipping_Zones::get_zones();
		// @codingStandardsIgnoreEnd

		update_option( 'woocommerce_ship_to_countries', '' );
		update_option( 'woocommerce_weight_unit', $weight_unit );
		update_option( 'woocommerce_dimension_unit', $dimension_unit );

		$setup_wcs_labels  = isset( $_POST['setup_woocommerce_services'] ) && 'yes' === $_POST['setup_woocommerce_services'];
		$setup_shipstation = isset( $_POST['setup_shipstation'] ) && 'yes' === $_POST['setup_shipstation'];

		update_option( 'woocommerce_setup_shipping_labels', $setup_wcs_labels );

		if ( $setup_wcs_labels ) {
			$this->install_woocommerce_services();
		}

		if ( $setup_shipstation ) {
			$this->install_plugin(
				'woocommerce-shipstation-integration',
				array(
					'name'      => __( 'ShipStation', 'woocommerce' ),
					'repo-slug' => 'woocommerce-shipstation-integration',
					'file'      => 'woocommerce-shipstation.php',
				)
			);
		}

		// For now, limit this setup to the first run.
		if ( ! empty( $existing_zones ) ) {
			wp_safe_redirect( esc_url_raw( $this->get_next_step_link() ) );
			exit;
		}

		/*
		 * If enabled, create a shipping zone containing the country the
		 * store is located in, with the selected method preconfigured.
		 */
		if ( $setup_domestic ) {
			$zone = new WC_Shipping_Zone( null );
			$zone->set_zone_order( 0 );
			$zone->add_location( WC()->countries->get_base_country(), 'country' );
			$zone_id = $zone->save();

			// Save chosen shipping method settings (using REST controller for convenience).
			if ( ! empty( $_POST['shipping_zones']['domestic'][ $domestic_method ] ) ) { // WPCS: input var ok.

				// Sanitize the cost field.
				$domestic_cost = wc_clean( wp_unslash( $_POST['shipping_zones']['domestic'][ $domestic_method ] ) );
				$domestic_cost = str_replace( array( get_woocommerce_currency_symbol(), html_entity_decode( get_woocommerce_currency_symbol() ) ), '', $domestic_cost );

				// Build and make a REST request to save the shipping zone and method set.
				$request = new WP_REST_Request( 'POST', "/wc/v3/shipping/zones/{$zone_id}/methods" );
				$request->add_header( 'Content-Type', 'application/json' );
				$request->set_body(
					wp_json_encode(
						array(
							'method_id' => $domestic_method,
							'settings'  => $domestic_cost,
						)
					)
				);
				rest_do_request( $request );
			}
		}

		// If enabled, set the selected method for the "rest of world" zone.
		if ( $setup_intl ) {
			// Save chosen shipping method settings (using REST controller for convenience).
			if ( ! empty( $_POST['shipping_zones']['intl'][ $intl_method ] ) ) { // WPCS: input var ok.

				// Sanitize the cost field.
				$intl_cost = wc_clean( wp_unslash( $_POST['shipping_zones']['intl'][ $intl_method ] ) );
				$intl_cost = str_replace( array( get_woocommerce_currency_symbol(), html_entity_decode( get_woocommerce_currency_symbol() ) ), '', $intl_cost );

				// Build and make a REST request to save the shipping zone and method set.
				$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/0/methods' );
				$request->add_header( 'Content-Type', 'application/json' );
				$request->set_body(
					wp_json_encode(
						array(
							'method_id' => $intl_method,
							'settings'  => $intl_cost,
						)
					)
				);
				rest_do_request( $request );
			}
		}

		// Notify the user that no shipping methods are configured.
		if ( ! $setup_domestic && ! $setup_intl ) {
			WC_Admin_Notices::add_notice( 'no_shipping_methods' );
		}

		wp_safe_redirect( esc_url_raw( $this->get_next_step_link() ) );
		exit;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.