Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate product endpoint args for item schema #46551

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Fix duplicate product endpoint args for item schema #46551
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function register_routes() {
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'duplicate_product' ),
'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' ),
)
Expand All @@ -99,10 +99,6 @@ public function duplicate_product( $request ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

if ( 'simple' !== $product->get_type() ) {
Copy link
Contributor Author

@octaedro octaedro Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because it's not necessary anymore.

$request['type'] = $product->get_type();
}

// Creating product object from request data in preparation for copying.
$updated_product = $this->prepare_object_for_database( $request );
$duplicated_product = ( new WC_Admin_Duplicate_Product() )->product_duplicate( $updated_product );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,33 @@ public function test_duplicate_product_with_extra_args() {
$this->assertEquals( 'new-sku', $duplicated_product->get_sku() );
$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() );
}
}