WC_REST_CRUD_Controller::create_item()
Create a single item.
Метод класса: WC_REST_CRUD_Controller{}
Хуки из метода
Возвращает
WP_Error|WP_REST_Response
.
Использование
$WC_REST_CRUD_Controller = new WC_REST_CRUD_Controller(); $WC_REST_CRUD_Controller->create_item( $request );
- $request(WP_REST_Request) (обязательный)
- Full details about the request.
Код WC_REST_CRUD_Controller::create_item() WC REST CRUD Controller::create item WC 9.3.3
public function create_item( $request ) { if ( ! empty( $request['id'] ) ) { /* translators: %s: post type */ return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); } $object = $this->save_object( $request, true ); 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, true ); } catch ( WC_Data_Exception $e ) { $object->delete(); return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); } catch ( WC_REST_Exception $e ) { $object->delete(); 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 ); $response = rest_ensure_response( $response ); $response->set_status( 201 ); $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) ); return $response; }