[REST API] Throw messages when trying to manipulate variations in products endpoint

This commit is contained in:
Claudio Sanches 2016-11-08 13:55:33 -02:00
parent e1b914d227
commit f23a529329
2 changed files with 9 additions and 3 deletions

View File

@ -142,7 +142,9 @@ abstract class WC_REST_Posts_Controller extends WC_REST_Controller {
$id = (int) $request['id']; $id = (int) $request['id'];
$post = get_post( $id ); $post = get_post( $id );
if ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) { 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_invalid_{$this->post_type}_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
} }
@ -247,7 +249,9 @@ abstract class WC_REST_Posts_Controller extends WC_REST_Controller {
$id = (int) $request['id']; $id = (int) $request['id'];
$post = get_post( $id ); $post = get_post( $id );
if ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) { 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 ) ); return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
} }

View File

@ -1786,7 +1786,9 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$force = (bool) $request['force']; $force = (bool) $request['force'];
$post = get_post( $id ); $post = get_post( $id );
if ( empty( $id ) || empty( $post->ID ) || $post->post_type !== $this->post_type ) { 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", __( 'Invalid post ID.', 'woocommerce' ), array( 'status' => 404 ) ); return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid post ID.', 'woocommerce' ), array( 'status' => 404 ) );
} }