Merge pull request #19598 from woocommerce/fix/19500

Handle manage_stock mixed content for variations
This commit is contained in:
Mike Jolley 2018-04-04 11:28:24 +01:00 committed by GitHub
commit 95d3266bfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 2 deletions

View File

@ -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' ),
),

View File

@ -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 );
}
}