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
Add API Rest endpoint to duplicate product #46141
Conversation
} | ||
|
||
if ( 'simple' !== $product->get_type() ) { | ||
$request['type'] = $product->get_type(); |
There was a problem hiding this comment.
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.
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: |
There was a problem hiding this 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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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 🎉
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
andstatus
are not editable.Closes #43282.
ACs
wc/v3/products/<product id>/duplicate
that creates and returns a new draft product copied from the passed in product id.draft
(Copy)
at the endHow to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:
Duplicate
andDuplicate and modify
. After pressing any of them, a prompt asking for aProduct 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
.Console
tab.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.(Copy)
at the endDraft
.Products > All Products
and find the duplicated product there.Products > All Products
. Verify that the variations were duplicated correctly (the prices set in step 1 should match).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".Changelog entry
Significance
Type
Message
Comment