Skip to content

Load product in default store before duplicating #23209

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

Conversation

JeroenVanLeusden
Copy link
Member

Description (*)

Duplicating a product will use the current scope data as default values instead of copying it from the original product.

Fixed Issues (if relevant)

  1. Duplicate product #15656: Duplicate product

Manual testing scenarios (*)

  1. Create product with different values in different StoreViews (e.g StoreView UK, NL, DE, ...)
  2. Open product and select one StoreView (e.g. UK - not "All StoreViews"!)
  3. Click on "Save product & duplicate"

Questions or comments

Not really happy with reloading the product while we have it already as method parameter. Maybe deprecate the copy() method and create a copyBySku() method?

Will update tests if implementation is approved by Magento.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Jun 11, 2019

Hi @JeroenVanLeusden. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

Copy link
Contributor

@dmytro-ch dmytro-ch left a comment

Choose a reason for hiding this comment

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

Hi @JeroenVanLeusden!

Not really happy with reloading the product while we have it already as method parameter. Maybe deprecate the copy() method and create a copyBySku() method?

I also agree, it would be better to pass the already correct product data as copy method parameter.
So we may try to load the product via repository in case the current_store_id value of the product is not equal to Store::DEFAULT_STORE_ID, instead of using the Product Builder in the following controller actions:

  • \Magento\Catalog\Controller\Adminhtml\Product\Duplicate::execute
  • \Magento\Catalog\Controller\Adminhtml\Product\Save::execute

The only problem, in my opinion, is that the product entity after loading does not contain all the necessary data. I guess, the bunch of data entries still need to be populated from request.

Thank you!

@JeroenVanLeusden
Copy link
Member Author

Hi @dmytro-ch,

I can refactor the \Magento\Catalog\Controller\Adminhtml\Product\Save::execute te be:

if ($redirectBack === 'duplicate') {
    if ($product->getStoreId() === \Magento\Store\Model\Store::DEFAULT_STORE_ID) {
        $product->unsetData('quantity_and_stock_status');
        $productToDuplicate = $product;
    } else {
        $productToDuplicate = $this->productRepository->get(
            $product->getSku(),
            false,
            \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            true
        );
    }

    $newProduct = $this->productCopier->copy($productToDuplicate);
    $this->messageManager->addSuccessMessage(__('You duplicated the product.'));
}

Although i'm not sure what you mean with:

in my opinion, is that the product entity after loading does not contain all the necessary data. I guess, the bunch of data entries still need to be populated from request.

The product is already saved so all the data we need should be there already when fetched using the repository?

Also, I've found no reference to \Magento\Catalog\Controller\Adminhtml\Product\Duplicate::execute. Can we deprecate this class?

@dmytro-ch
Copy link
Contributor

@JeroenVanLeusden,
thank you for the update.

The new solution looks correct to me.

Although i'm not sure what you mean with:

in my opinion, is that the product entity after loading does not contain all the necessary data. I guess, the bunch of data entries still need to be populated from request.

The product is already saved so all the data we need should be there already when fetched using the repository?

I mean if you compare the results of $product->getData() and $productToDuplicate->getData() according to your code snippet, you may get the different number of values in resulting array. We just need to make sure the duplicated product does not lose any data.

Also, I've found no reference to \Magento\Catalog\Controller\Adminhtml\Product\Duplicate::execute.
Can we deprecate this class?

Sure

@JeroenVanLeusden JeroenVanLeusden force-pushed the JeroenVanLeusden-patch-1 branch from 1e503ea to 50bd097 Compare June 24, 2019 13:52
@JeroenVanLeusden
Copy link
Member Author

Hi @dmytro-ch,

I've updated the PR, still using the repository to load the product because it duplicates just fine. Are there some additional tests I can do to make sure it's all good?

@magento-engcom-team
Copy link
Contributor

Hi @sidolov, thank you for the review.
ENGCOM-5689 has been created to process this Pull Request
✳️ @sidolov, could you please add one of the following labels to the Pull Request?

Label Description
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests

@engcom-Delta
Copy link
Contributor

Hi @JeroenVanLeusden during testing PR changes I found issue with product duplicate when different URL Key is set for each store view.
Manual testing scenario:

  • Create more than one store view:
    image
  • Create product
  • Edit product for each store view and save:
    • Default Store View:
      • Product Name=simple1 default
      • URL Key=simple1-def
    • Second View:
      • Product Name=simple1 second
      • URL Key=simple1-second
    • Third View:
      • Product Name=simple1 third
      • URL Key=simple1-third
    • Notice: Create Permanent Redirect for old URL is checked on each store view when URL key is edited
      image
  • Open product and select Second View
  • Click on "Save product & duplicate"

Result:
❌ Product Name value is set to All Store Views(e.g., "simple1") value for each store view
#23209PRDuplicate
✔️ URL keys is generated correctly using store view value for it
#23209PRURLKey

Could you take a look?

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

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

Hi @JeroenVanLeusden,

This PR is really closely related to #24505 and looks like changes from this PR could fix issue that described @engcom-Delta.
Could you collaborate with @webkul-paul to fix both these issues?

@@ -6,6 +6,9 @@
*/
namespace Magento\Bundle\Controller\Adminhtml\Bundle\Product\Edit;

/**
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add description why it became deprecated and add @see tag with example what we should use instead

@@ -242,6 +242,7 @@ public function getProductSetId()

/**
* @return string
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add description why it became deprecated and add @see tag with example what we should use instead

@@ -9,6 +9,9 @@
use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;

/**
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add description why it became deprecated and add @see tag with example what we should use instead

@@ -6,6 +6,9 @@
*/
namespace Magento\Downloadable\Controller\Adminhtml\Downloadable\Product\Edit;

/**
* @deprecated
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add description why it became deprecated and add @see tag with example what we should use instead

@ghost ghost assigned ihor-sviziev Sep 11, 2019
@sidolov
Copy link
Contributor

sidolov commented Sep 16, 2019

@JeroenVanLeusden , I am closing this PR now due to inactivity.
Please reopen and update if you wish to continue.
Thank you for the collaboration!

@sidolov sidolov closed this Sep 16, 2019
@m2-assistant
Copy link

m2-assistant bot commented Sep 16, 2019

Hi @JeroenVanLeusden, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants