Automattic\WooCommerce\Internal\RestApi\Routes\V4\ShippingZones

ShippingZoneService::update_shipping_zonepublicWC 1.0

Update an existing shipping zone.

Method of the class: ShippingZoneService{}

No Hooks.

Returns

WC_Shipping_Zone|WP_Error. Updated zone object on success, WP_Error on failure.

Usage

$ShippingZoneService = new ShippingZoneService();
$ShippingZoneService->update_shipping_zone( $zone, $params );
$zone(WC_Shipping_Zone) (required)
Zone object to update.
$params(array) (required)

Zone parameters to update. All parameters are optional.

  • name(string)
    Zone name. Cannot be changed for "Rest of the World" zone (ID 0).

  • order(int)
    Zone order for sorting. Cannot be changed for "Rest of the World" zone.

  • locations(array)
    Array of location objects. Cannot be changed for "Rest of the World" zone. Each location should have 'code' (string) and 'type' (string) keys. Valid types: 'postcode', 'state', 'country', 'continent'.

ShippingZoneService::update_shipping_zone() code WC 10.4.3

public function update_shipping_zone( $zone, $params ) {
	$params = wp_parse_args(
		$params,
		array(
			'name'      => null,
			'order'     => null,
			'locations' => null,
		)
	);

	$is_rest_of_world = 0 === $zone->get_id();

	if ( ! is_null( $params['name'] ) ) {
		if ( $is_rest_of_world ) {
			return new WP_Error(
				'woocommerce_rest_cannot_edit_zone',
				__( 'Cannot change name of "Rest of the World" zone.', 'woocommerce' ),
				array( 'status' => WP_Http::BAD_REQUEST )
			);
		}

		$name = trim( $params['name'] );
		if ( '' === $name ) {
			return new WP_Error(
				'woocommerce_rest_invalid_zone_name',
				__( 'Zone name cannot be empty.', 'woocommerce' ),
				array( 'status' => WP_Http::BAD_REQUEST )
			);
		}
		$zone->set_zone_name( $name );
	}

	if ( ! is_null( $params['order'] ) ) {
		if ( $is_rest_of_world ) {
			return new WP_Error(
				'woocommerce_rest_cannot_edit_zone',
				__( 'Cannot change order of "Rest of the World" zone.', 'woocommerce' ),
				array( 'status' => WP_Http::BAD_REQUEST )
			);
		}
		$zone->set_zone_order( $params['order'] );
	}

	$locations_being_cleared = false;
	if ( ! is_null( $params['locations'] ) ) {
		if ( $is_rest_of_world ) {
			return new WP_Error(
				'woocommerce_rest_cannot_edit_zone',
				__( 'Cannot change locations of "Rest of the World" zone.', 'woocommerce' ),
				array( 'status' => WP_Http::BAD_REQUEST )
			);
		}
		$raw_locations = $params['locations'];
		$locations     = array();

		foreach ( (array) $raw_locations as $raw_location ) {
			$locations_being_cleared = false;
			if ( empty( $raw_location['code'] ) ) {
				continue;
			}

			$type = ! empty( $raw_location['type'] ) ? $raw_location['type'] : 'country';

			// Normalize 'country:state' to 'state' for v4 API backward compatibility.
			if ( 'country:state' === $type ) {
				$type = 'state';
			}

			if ( ! $zone->is_valid_location_type( $type ) ) {
				continue;
			}

			$locations[] = array(
				'code' => $raw_location['code'],
				'type' => $type,
			);
		}

		$locations_being_cleared = empty( $locations );

		$zone->set_locations( $locations );
	}

	$zone->save();

	// WORKAROUND: WC_Data::apply_changes() uses array_replace_recursive() which doesn't
	// properly clear array properties when set to empty arrays. After save(), get_zone_locations()
	// returns stale cached data. Only reload when clearing locations to get accurate state.
	if ( $locations_being_cleared ) {
		$zone = WC_Shipping_Zones::get_zone( $zone->get_id() );
	}

	return $zone;
}