| @@ 688-716 (lines=29) @@ | ||
| 685 | * @param WP_REST_Request $request Full details about the request. |
|
| 686 | * @return WP_Error|WP_REST_Response |
|
| 687 | */ |
|
| 688 | public function create_item( $request ) { |
|
| 689 | if ( ! empty( $request['id'] ) ) { |
|
| 690 | return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); |
|
| 691 | } |
|
| 692 | ||
| 693 | $order_id = $this->create_order( $request ); |
|
| 694 | if ( is_wp_error( $order_id ) ) { |
|
| 695 | return $order_id; |
|
| 696 | } |
|
| 697 | ||
| 698 | $post = get_post( $order_id ); |
|
| 699 | $this->update_additional_fields_for_object( $post, $request ); |
|
| 700 | ||
| 701 | /** |
|
| 702 | * Fires after a single item is created or updated via the REST API. |
|
| 703 | * |
|
| 704 | * @param object $post Inserted object (not a WP_Post object). |
|
| 705 | * @param WP_REST_Request $request Request object. |
|
| 706 | * @param boolean $creating True when creating item, false when updating. |
|
| 707 | */ |
|
| 708 | do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true ); |
|
| 709 | $request->set_param( 'context', 'edit' ); |
|
| 710 | $response = $this->prepare_item_for_response( $post, $request ); |
|
| 711 | $response = rest_ensure_response( $response ); |
|
| 712 | $response->set_status( 201 ); |
|
| 713 | $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) ); |
|
| 714 | ||
| 715 | return $response; |
|
| 716 | } |
|
| 717 | ||
| 718 | /** |
|
| 719 | * Update a single order. |
|
| @@ 269-299 (lines=31) @@ | ||
| 266 | * @param WP_REST_Request $request Full details about the request. |
|
| 267 | * @return WP_Error|WP_REST_Response |
|
| 268 | */ |
|
| 269 | public function create_item( $request ) { |
|
| 270 | if ( ! empty( $request['id'] ) ) { |
|
| 271 | return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); |
|
| 272 | } |
|
| 273 | ||
| 274 | $coupon_id = $this->save_coupon( $request ); |
|
| 275 | if ( is_wp_error( $coupon_id ) ) { |
|
| 276 | return $coupon_id; |
|
| 277 | } |
|
| 278 | ||
| 279 | $post = get_post( $coupon_id ); |
|
| 280 | $this->update_additional_fields_for_object( $post, $request ); |
|
| 281 | ||
| 282 | $this->add_post_meta_fields( $post, $request ); |
|
| 283 | ||
| 284 | /** |
|
| 285 | * Fires after a single item is created or updated via the REST API. |
|
| 286 | * |
|
| 287 | * @param object $post Inserted object (not a WP_Post object). |
|
| 288 | * @param WP_REST_Request $request Request object. |
|
| 289 | * @param boolean $creating True when creating item, false when updating. |
|
| 290 | */ |
|
| 291 | do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true ); |
|
| 292 | $request->set_param( 'context', 'edit' ); |
|
| 293 | $response = $this->prepare_item_for_response( $post, $request ); |
|
| 294 | $response = rest_ensure_response( $response ); |
|
| 295 | $response->set_status( 201 ); |
|
| 296 | $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) ); |
|
| 297 | ||
| 298 | return $response; |
|
| 299 | } |
|
| 300 | ||
| 301 | /** |
|
| 302 | * Update a single coupon. |
|