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

Add API Rest endpoint to duplicate product #46141

Merged
merged 10 commits into from Apr 8, 2024

Conversation

octaedro
Copy link
Contributor

@octaedro octaedro commented Apr 2, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR adds an endpoint to the API Rest that allows duplicating a product and saving it as a draft.
It's also possible to update the product when extra parameters are sent.
Product type and status are not editable.

Closes #43282.

ACs

Edit tasklist title
Beta Give feedback Tasklist ACs, more options

Delete tasklist

Delete tasklist block?
Are you sure? All relationships in this tasklist will be removed.
  1. Create new endpoint POST wc/v3/products/<product id>/duplicate that creates and returns a new draft product copied from the passed in product id.
    Options
  2. Endpoint should support the status ( or potential other product data ) as part of the POST and adhere to that. Status will default to draft
    Options
  3. Name should include (Copy) at the end
    Options
  4. For variable product we should copy over the variations as well ( the fixed amount, similar as the classic experience, we will have a follow up issue for supporting variations above the limit >100 ).
    Options
  5. Add PHP tests
    Options

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Create a simple and a variable product and write down their IDs. In the case of the variable product, set a price to all the variations.
  2. Install this plugin and activate it.
  3. The plugin will show two buttons, Duplicate and Duplicate and modify. After pressing any of them, a prompt asking for a Product ID to duplicate will be visible. After adding a product ID and pressing ok, it will send a request to /wp-json/wc/v3/products/[your-product-id]/duplicate.
  4. Open the browser's dev tools and go to the Console tab.
  5. Press Duplicate and add the ID of the simple product to the prompt. If everything goes well you'll see the duplicated product printed in the console.
  6. Verify that the new product has:
  • A new ID
  • The product's name says (Copy) at the end
  • The product status is Draft.
  1. You can also go to Products > All Products and find the duplicated product there.
  2. Repeat step 5 with the variable product ID.
  3. Select the duplicated product from Products > All Products. Verify that the variations were duplicated correctly (the prices set in step 1 should match).
  4. Now press Duplicate and modify and add the ID of the simple product to the prompt. If everything goes well you'll see the duplicated product printed in the console. This option also sends a description of the product, so the duplicated product's description should say: "This is my description".
  5. Repeat step 10 with a variable product.

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Apr 2, 2024
}

if ( 'simple' !== $product->get_type() ) {
$request['type'] = $product->get_type();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to update the type in the request to match the product type. Otherwise, it would be taken as a simple product.

@octaedro octaedro self-assigned this Apr 5, 2024
@octaedro octaedro requested a review from a team April 5, 2024 13:04
Copy link
Contributor

github-actions bot commented Apr 5, 2024

Hi @louwie17,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@octaedro octaedro marked this pull request as ready for review April 5, 2024 13:20
louwie17
louwie17 previously approved these changes Apr 8, 2024
Copy link
Contributor

@louwie17 louwie17 left a comment

Choose a reason for hiding this comment

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

This tested well, and code looks quite good. I did leave one inline comment with a question around the implementation and a suggestion of adding an inline comment.

Let me know what you think?

}

$updated_product = $this->prepare_object_for_database( $request );
$duplicated_product = ( new WC_Admin_Duplicate_Product() )->product_duplicate( $updated_product );
Copy link
Contributor

Choose a reason for hiding this comment

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

The use of prepare_object_for_database( $request ) to get a product object that we than copy is clever, but also feels a bit weird to me.
Is this something we have done in the past?

I am ok with leaving this, but maybe just adding an inline comment would be helpful, something like:
// Creating product object from request data in preparation for copying.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it seems like a naming problem.
I'm good with adding the comment, but if you prefer, I can add a parameter to prepare_object_for_database and use the ( new WC_Admin_Duplicate_Product() )->product_duplicate( $product ) inside.

I'm leaning towards the first solution (adding a comment) because the duplication at the end is a way to save the product (slightly modified), so it would keep the pattern of preparing then saving the product.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@louwie17 I just added the comment in the commit 290345e

@louwie17 louwie17 dismissed their stale review April 8, 2024 11:26

Did not mean to approve

@octaedro octaedro requested a review from louwie17 April 8, 2024 13:36
Copy link
Contributor

@louwie17 louwie17 left a comment

Choose a reason for hiding this comment

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

Thanks for adding the comment, this looks good, thanks @octaedro 🎉

@octaedro octaedro merged commit 46b07c0 into trunk Apr 8, 2024
26 checks passed
@octaedro octaedro deleted the dev/43282_create_api_endpoint_duplicate_product branch April 8, 2024 14:14
@github-actions github-actions bot added this to the 8.9.0 milestone Apr 8, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Apr 8, 2024
@nigeljamesstevenson nigeljamesstevenson added contains: rest api change Indicates if the PR contains a REST API change. status: analysis complete Indicates if a PR has been analysed by Solaris needs: internal testing Indicates if the PR requires further testing conducted by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contains: rest api change Indicates if the PR contains a REST API change. needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create API endpoint to duplicate a certain product
3 participants