Fix duplicate product endpoint args for item schema (#46551)
* Fix endpoint args for item schema * Add tests * Add changelog
This commit is contained in:
parent
a15fae2f99
commit
25eafffcae
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: fix
|
||||||
|
|
||||||
|
Fix duplicate product endpoint args for item schema #46551
|
|
@ -77,7 +77,7 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
||||||
'methods' => WP_REST_Server::CREATABLE,
|
'methods' => WP_REST_Server::CREATABLE,
|
||||||
'callback' => array( $this, 'duplicate_product' ),
|
'callback' => array( $this, 'duplicate_product' ),
|
||||||
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
'permission_callback' => array( $this, 'create_item_permissions_check' ),
|
||||||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
|
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
||||||
),
|
),
|
||||||
'schema' => array( $this, 'get_public_item_schema' ),
|
'schema' => array( $this, 'get_public_item_schema' ),
|
||||||
)
|
)
|
||||||
|
@ -99,10 +99,6 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
||||||
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'simple' !== $product->get_type() ) {
|
|
||||||
$request['type'] = $product->get_type();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creating product object from request data in preparation for copying.
|
// Creating product object from request data in preparation for copying.
|
||||||
$updated_product = $this->prepare_object_for_database( $request );
|
$updated_product = $this->prepare_object_for_database( $request );
|
||||||
$duplicated_product = ( new WC_Admin_Duplicate_Product() )->product_duplicate( $updated_product );
|
$duplicated_product = ( new WC_Admin_Duplicate_Product() )->product_duplicate( $updated_product );
|
||||||
|
|
|
@ -507,4 +507,33 @@ class WC_REST_Products_Controller_Tests extends WC_REST_Unit_Test_Case {
|
||||||
$this->assertEquals( 'new-sku', $duplicated_product->get_sku() );
|
$this->assertEquals( 'new-sku', $duplicated_product->get_sku() );
|
||||||
$this->assertEquals( 'test', $duplicated_product->get_meta( 'test', true ) );
|
$this->assertEquals( 'test', $duplicated_product->get_meta( 'test', true ) );
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Test the duplicate product endpoint with to update product's name and stock management.
|
||||||
|
*/
|
||||||
|
public function test_duplicate_product_with_extra_args_name_stock_management() {
|
||||||
|
$product = WC_Helper_Product::create_simple_product(
|
||||||
|
true,
|
||||||
|
array(
|
||||||
|
'name' => 'Blueberry Cake',
|
||||||
|
'sku' => 'blueberry-cake-1',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$product_id = $product->get_id();
|
||||||
|
|
||||||
|
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product_id . '/duplicate' );
|
||||||
|
$request->set_param( 'name', 'new-name' );
|
||||||
|
$request->set_param( 'manage_stock', true );
|
||||||
|
$request->set_param( 'stock_quantity', 10 );
|
||||||
|
$response = $this->server->dispatch( $request );
|
||||||
|
$this->assertEquals( 200, $response->get_status() );
|
||||||
|
|
||||||
|
$response_data = $response->get_data();
|
||||||
|
$this->assertArrayHasKey( 'id', $response_data );
|
||||||
|
$this->assertNotEquals( $product_id, $response_data['id'] );
|
||||||
|
|
||||||
|
$duplicated_product = wc_get_product( $response_data['id'] );
|
||||||
|
$this->assertEquals( 'new-name (Copy)', $duplicated_product->get_name() );
|
||||||
|
$this->assertTrue( $duplicated_product->get_manage_stock() );
|
||||||
|
$this->assertEquals( 10, $duplicated_product->get_stock_quantity() );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue