WC_REST_Shipping_Zone_Locations_V2_Controller::update_items()publicWC 1.0

Update all Shipping Zone Locations.

Метод класса: WC_REST_Shipping_Zone_Locations_V2_Controller{}

Хуков нет.

Возвращает

WP_REST_Response|WP_Error.

Использование

$WC_REST_Shipping_Zone_Locations_V2_Controller = new WC_REST_Shipping_Zone_Locations_V2_Controller();
$WC_REST_Shipping_Zone_Locations_V2_Controller->update_items( $request );
$request(WP_REST_Request) (обязательный)
Request data.

Код WC_REST_Shipping_Zone_Locations_V2_Controller::update_items() WC 8.7.0

public function update_items( $request ) {
	$zone = $this->get_zone( (int) $request['id'] );

	if ( is_wp_error( $zone ) ) {
		return $zone;
	}

	if ( 0 === $zone->get_id() ) {
		return new WP_Error( 'woocommerce_rest_shipping_zone_locations_invalid_zone', __( 'The "locations not covered by your other zones" zone cannot be updated.', 'woocommerce' ), array( 'status' => 403 ) );
	}

	$raw_locations = $request->get_json_params();
	$locations     = array();

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

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

		if ( ! in_array( $type, array( 'postcode', 'state', 'country', 'continent' ), true ) ) {
			continue;
		}

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

	$zone->set_locations( $locations );
	$zone->save();

	return $this->get_items( $request );
}