diff --git a/includes/api/class-wc-rest-product-variations-controller.php b/includes/api/class-wc-rest-product-variations-controller.php index a87bd49b56f..16a34e42e95 100644 --- a/includes/api/class-wc-rest-product-variations-controller.php +++ b/includes/api/class-wc-rest-product-variations-controller.php @@ -335,7 +335,11 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller // Stock handling. if ( isset( $request['manage_stock'] ) ) { - $variation->set_manage_stock( $request['manage_stock'] ); + if ( 'parent' === $request['manage_stock'] ) { + $variation->set_manage_stock( false ); // This just indicates the variation does not manage stock, but the parent does. + } else { + $variation->set_manage_stock( wc_string_to_bool( $request['manage_stock'] ) ); + } } if ( isset( $request['in_stock'] ) ) { @@ -802,7 +806,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller ), 'manage_stock' => array( 'description' => __( 'Stock management at variation level.', 'woocommerce' ), - 'type' => 'boolean', + 'type' => 'mixed', 'default' => false, 'context' => array( 'view', 'edit' ), ), diff --git a/tests/unit-tests/api/product-variations.php b/tests/unit-tests/api/product-variations.php index 579f6f0aa92..6e5731fc181 100644 --- a/tests/unit-tests/api/product-variations.php +++ b/tests/unit-tests/api/product-variations.php @@ -382,4 +382,59 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case { $product->delete( true ); } + /** + * Test updating a variation stock. + * + * @since 3.0.0 + */ + public function test_update_variation_manage_stock() { + wp_set_current_user( $this->user ); + + $product = WC_Helper_Product::create_variation_product(); + $product->set_manage_stock( false ); + $product->save(); + + $children = $product->get_children(); + $variation_id = $children[0]; + + // Set stock to true. + $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); + $request->set_body_params( array( + 'manage_stock' => true, + ) ); + + $response = $this->server->dispatch( $request ); + $variation = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( true, $variation['manage_stock'] ); + + // Set stock to false. + $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); + $request->set_body_params( array( + 'manage_stock' => false, + ) ); + + $response = $this->server->dispatch( $request ); + $variation = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( false, $variation['manage_stock'] ); + + // Set stock to false but parent is managing stock. + $product->set_manage_stock( true ); + $product->save(); + $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); + $request->set_body_params( array( + 'manage_stock' => false, + ) ); + + $response = $this->server->dispatch( $request ); + $variation = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( 'parent', $variation['manage_stock'] ); + + $product->delete( true ); + } }