WC_REST_Posts_Controller::update_item()publicWC 1.0

Update a single post.

Method of the class: WC_REST_Posts_Controller{}

Hooks from the method

Return

WP_Error|WP_REST_Response.

Usage

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

WC_REST_Posts_Controller::update_item() code WC 8.6.1

public function update_item( $request ) {
	$id   = (int) $request['id'];
	$post = get_post( $id );

	if ( ! empty( $post->post_type ) && 'product_variation' === $post->post_type && 'product' === $this->post_type ) {
		return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array( 'status' => 404 ) );
	} elseif ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) {
		return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
	}

	$post = $this->prepare_item_for_database( $request );
	if ( is_wp_error( $post ) ) {
		return $post;
	}
	// Convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
	$post_id = wp_update_post( (array) $post, true );
	if ( is_wp_error( $post_id ) ) {
		if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) {
			$post_id->add_data( array( 'status' => 500 ) );
		} else {
			$post_id->add_data( array( 'status' => 400 ) );
		}
		return $post_id;
	}

	$post = get_post( $post_id );
	$this->update_additional_fields_for_object( $post, $request );

	// Update meta fields.
	$meta_fields = $this->update_post_meta_fields( $post, $request );
	if ( is_wp_error( $meta_fields ) ) {
		return $meta_fields;
	}

	/**
	 * Fires after a single item is created or updated via the REST API.
	 *
	 * @param WP_Post         $post      Post object.
	 * @param WP_REST_Request $request   Request object.
	 * @param boolean         $creating  True when creating item, false when updating.
	 */
	do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );

	$request->set_param( 'context', 'edit' );
	$response = $this->prepare_item_for_response( $post, $request );
	return rest_ensure_response( $response );
}