WC_REST_CRUD_Controller::update_item() public WC 1.0
Update a single post.
{} It's a method of the class: WC_REST_CRUD_Controller{}
Hooks from the method
Return
WP_Error|WP_REST_Response.
Usage
$WC_REST_CRUD_Controller = new WC_REST_CRUD_Controller(); $WC_REST_CRUD_Controller->update_item( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
Code of WC_REST_CRUD_Controller::update_item() WC REST CRUD Controller::update item WC 5.0.0
public function update_item( $request ) {
$object = $this->get_object( (int) $request['id'] );
if ( ! $object || 0 === $object->get_id() ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 400 ) );
}
$object = $this->save_object( $request, false );
if ( is_wp_error( $object ) ) {
return $object;
}
try {
$this->update_additional_fields_for_object( $object, $request );
/**
* Fires after a single object is created or updated via the REST API.
*
* @param WC_Data $object Inserted object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating object, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, false );
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$request->set_param( 'context', 'edit' );
$response = $this->prepare_object_for_response( $object, $request );
return rest_ensure_response( $response );
}