WC_REST_Shipping_Zone_Methods_V2_Controller::create_item()publicWC 1.0

Create a new shipping zone method instance.

Method of the class: WC_REST_Shipping_Zone_Methods_V2_Controller{}

No Hooks.

Return

WP_REST_Request|WP_Error.

Usage

$WC_REST_Shipping_Zone_Methods_V2_Controller = new WC_REST_Shipping_Zone_Methods_V2_Controller();
$WC_REST_Shipping_Zone_Methods_V2_Controller->create_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

WC_REST_Shipping_Zone_Methods_V2_Controller::create_item() code WC 8.7.0

public function create_item( $request ) {
	$method_id = $request['method_id'];
	$zone      = $this->get_zone( $request['zone_id'] );
	if ( is_wp_error( $zone ) ) {
		return $zone;
	}

	$instance_id = $zone->add_shipping_method( $method_id );
	$methods     = $zone->get_shipping_methods();
	$method      = false;
	foreach ( $methods as $method_obj ) {
		if ( $instance_id === $method_obj->instance_id ) {
			$method = $method_obj;
			break;
		}
	}

	if ( false === $method ) {
		return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( 'Resource cannot be created.', 'woocommerce' ), array( 'status' => 500 ) );
	}

	$method = $this->update_fields( $instance_id, $method, $request );
	if ( is_wp_error( $method ) ) {
		return $method;
	}

	$data = $this->prepare_item_for_response( $method, $request );
	return rest_ensure_response( $data );
}