Skip to content

Commit

Permalink
Merge pull request woocommerce#18413 from woocommerce/fix/18377
Browse files Browse the repository at this point in the history
Rest API - Orders should be created for users who exist on the site only.
  • Loading branch information
claudiosanches committed Jan 10, 2018
2 parents 6630fca + ee6794f commit 76f66fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
11 changes: 9 additions & 2 deletions includes/api/class-wc-rest-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,19 @@ protected function save_object( $request, $creating = false ) {
return $object;
}

if ( $creating ) {
if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] ) {
// Make sure customer exists.
if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] && false === get_user_by( 'id', $request['customer_id'] ) ) {
if ( false === get_user_by( 'id', $request['customer_id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id',__( 'Customer ID is invalid.', 'woocommerce' ), 400 );
}

// Make sure customer is part of blog.
if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id_network',__( 'Customer ID does not belong to this site.', 'woocommerce' ), 400 );
}
}

if ( $creating ) {
$object->set_created_via( 'rest-api' );
$object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$object->calculate_totals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ protected function create_order( $request ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id',__( 'Customer ID is invalid.', 'woocommerce' ), 400 );
}

// Make sure customer is part of blog.
if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id_network',__( 'Customer ID does not belong to this site.', 'woocommerce' ), 400 );
}

$order = $this->prepare_item_for_database( $request );
$order->set_created_via( 'rest-api' );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
Expand Down
5 changes: 5 additions & 0 deletions includes/api/v1/class-wc-rest-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@ protected function create_order( $request ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id',__( 'Customer ID is invalid.', 'woocommerce' ), 400 );
}

// Make sure customer is part of blog.
if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id_network',__( 'Customer ID does not belong to this site.', 'woocommerce' ), 400 );
}

$order = $this->prepare_item_for_database( $request );
$order->set_created_via( 'rest-api' );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ public function has_downloadable_item() {
if ( $item->is_type( 'line_item' ) ) {
$product = $item->get_product();

if ( $product->has_file() ) {
if ( $product && $product->has_file() ) {
return true;
}
}
Expand Down

0 comments on commit 76f66fd

Please sign in to comment.