Hello,
Thank you for pointing that out. I haven't tested with the UNIQUE constraint, because I thought it should work, but it looks like the product was saved once more before that line.
Here is the correct solution:
On the line 275 above, add the following:
$shopProduct->reference = $duplicatedProduct->reference;
Also, on line 304 - old line 303, insert a new line and add the following:
$duplicatedObject->reference = bin2hex(random_bytes(10));
With these two changes, things should be working as expected.
I added two screenshots that also highlight where the code should be placed.