magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.

Home Page:http://www.magento.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicated product with related products is not working

lexbeelen opened this issue · comments

commented

Preconditions

  1. Magento ver. 2.1.1
  2. PHP 5.6
  3. MySQL 5.6

Steps to reproduce

  1. Go to admin > products > catalog
  2. Choose for a product that has related products
  3. Click on Save & Duplicate
  4. You're getting the messages Product SKU 123 "is not linked to product SKU '456-1 ' (456 is the related product)

Expected result

  1. Duplicated the product with the linked related products

Actual result

schermafbeelding 2016-09-27 om 12 27 58

Hi @lexbeelen, thank for reports

We've created internal ticket MAGETWO-59141 to address this issue.

commented

I have created a extension to fix this problem. https://github.com/lexbeelen/Magento2_FixDuplicate

@lexbeelen thank you for posting the issue. We are experiencing exactly the same thing, but unfortunately your posted extension did not resolve it. Do you have any additional suggestions you came across in diagnosing the problem?
EDIT: Sorry, did not execute installation properly. Works great, thanks so much!

I had this same on:

  1. Magento 2.1.2
  2. PHP 7.0.8
  3. MySQL 5.6

and this pull request solve the problem.

commented

The extension has been moved and updated.
https://github.com/weprovide/magento2-module-fix-duplicate

@lexbeelen Hi Lex, does this fix keep the related products intact?

hi @wigman ,
I tested the fix and works fine for me.
thanks a lot @lexbeelen for providing the extension

Hi @lexbeelen ,
I used this extension https://github.com/weprovide/magento2-module-fix-duplicate and got the 500 internal server error. As it goes for infinite loop.

I found some issue in my multistore website. Magento\Catalog\Model\Product\Copier.php function copy()


do {
    $urlKey = $duplicate->getUrlKey();
    $urlKey = preg_match('/(.*)-(\d+)$/', $urlKey, $matches)
        ? $matches[1] . '-' . ($matches[2] + 1)
        : $urlKey . '-1';
    $duplicate->setUrlKey($urlKey);
    try {
        $duplicate->save();
        $isDuplicateSaved = true;
    } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
    }
} while (!$isDuplicateSaved);

here loop is going to infinite and trying to create again again new url_key, but if I set

$duplicate->setUrlPath($urlKey); or $duplicate->setUrlPath(null); 
after 
$duplicate->setUrlKey($urlKey); 

then product will be duplicated but my bad luck duplicated product gives 404 error in frontend.

as I found url_path logic in ProductUrlPathGenerator.php by namespace Magento\CatalogUrlRewrite\Model function getUrlPath()


public function getUrlPath($product, $category = null)
{
    $path = $product->getData('url_path');
    if ($path === null) {
        $path = $product->getUrlKey()
            ? $this->prepareProductUrlKey($product)
            : $this->prepareProductDefaultUrlKey($product);
    }
    return $category === null
        ? $path
        : $this->categoryUrlPathGenerator->getUrlPath($category) . '/' . $path;
}

and my product has already url_path which is old product's url_path not newly created path. I am not sure where that url_path is comming and why we will use this ?

suppose my old product has url_key joust-duffle-bag and duplicated urk_key will be joust-duffle-bag-1 which is correct I am getting that but the main issue is url_path that is:

old product has url_path joust-duffle-bag and duplicated url_path will be joust-duffle-bag which is same (already exist) and the condition is there if ($path === null) there will generate new one else will take that old path value and this old path value is trying to save as url_key for duplicated product which throwing error "URL key for specified store already exists" as it trying to save again again same key even it trying to create new one due to of that url_path.

I don't understand how to avoid this url_path. I have also tried to truncate url_rewrite table still getting same issue.

@lexbeelen, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.3.0-dev, 2.2.0, 2.1.9