From 48fb4433acedcaf8db244cdb8defd6f15b85244e Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 18 Dec 2019 13:25:01 -0300 Subject: [PATCH] Allow edit line items without passing an product ID again --- .../class-wc-rest-orders-v1-controller.php | 14 ++++++++------ .../class-wc-rest-orders-v2-controller.php | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Controllers/Version1/class-wc-rest-orders-v1-controller.php b/src/Controllers/Version1/class-wc-rest-orders-v1-controller.php index 34570cda3d7..7ea86c16542 100644 --- a/src/Controllers/Version1/class-wc-rest-orders-v1-controller.php +++ b/src/Controllers/Version1/class-wc-rest-orders-v1-controller.php @@ -605,18 +605,20 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller { /** * Gets the product ID from the SKU or posted ID. * - * @param array $posted Request data - * + * @throws WC_REST_Exception When SKU or ID is not valid. + * @param array $posted Request data. + * @param string $action 'create' to add line item or 'update' to update it. * @return int - * @throws WC_REST_Exception */ - protected function get_product_id( $posted ) { + protected function get_product_id( $posted, $action = 'create' ) { if ( ! empty( $posted['sku'] ) ) { $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] ); } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) { $product_id = (int) $posted['product_id']; } elseif ( ! empty( $posted['variation_id'] ) ) { $product_id = (int) $posted['variation_id']; + } elseif ( 'update' === $action ) { + $product_id = 0; } else { throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce-rest-api' ), 400 ); } @@ -658,9 +660,9 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller { */ protected function prepare_line_items( $posted, $action = 'create' ) { $item = new WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' ); - $product = wc_get_product( $this->get_product_id( $posted ) ); + $product = wc_get_product( $this->get_product_id( $posted, $item->get_product() ) ); - if ( $product !== $item->get_product() ) { + if ( $product && $product !== $item->get_product() ) { $item->set_product( $product ); if ( 'create' === $action ) { diff --git a/src/Controllers/Version2/class-wc-rest-orders-v2-controller.php b/src/Controllers/Version2/class-wc-rest-orders-v2-controller.php index 8b7811c9f6f..66b19fbe528 100644 --- a/src/Controllers/Version2/class-wc-rest-orders-v2-controller.php +++ b/src/Controllers/Version2/class-wc-rest-orders-v2-controller.php @@ -591,18 +591,18 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller { * * @throws WC_REST_Exception When SKU or ID is not valid. * @param array $posted Request data. - * @param WC_Product|bool $product Product data. + * @param string $action 'create' to add line item or 'update' to update it. * @return int */ - protected function get_product_id( $posted, $product ) { + protected function get_product_id( $posted, $action = 'create' ) { if ( ! empty( $posted['sku'] ) ) { $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] ); } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) { $product_id = (int) $posted['product_id']; } elseif ( ! empty( $posted['variation_id'] ) ) { $product_id = (int) $posted['variation_id']; - } elseif ( $product && 0 < $product->get_id() ) { - return $product->get_id(); + } elseif ( 'update' === $action ) { + $product_id = 0; } else { throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce-rest-api' ), 400 ); } @@ -663,9 +663,9 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller { */ protected function prepare_line_items( $posted, $action = 'create', $item = null ) { $item = is_null( $item ) ? new WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; - $product = wc_get_product( $this->get_product_id( $posted, $item->get_product() ) ); + $product = wc_get_product( $this->get_product_id( $posted, $action ) ); - if ( $product !== $item->get_product() ) { + if ( $product && $product !== $item->get_product() ) { $item->set_product( $product ); if ( 'create' === $action ) {