Jump to content

Duplicate product WITHOUT copying reference field ?


Recommended Posts

Hello,

For integrity reason, I made the 'reference' column UNIQUE in ps_product table. As a consequence , when I duplicate a product I have en error because the new product has the same reference as the original one. Is it possible to change something in code, so that the reference field is not copied.   Thank you in advance.

 

Link to comment
Share on other sites

  • 3 years later...

Hello,

In PrestaShop 8 you can edit the following file: src/Adapter/Product/Update/ProductDuplicator.php

And on line 275 add a new empty line (do not delete the old one, just insert a new line - see screenshot below).

The code placed on the new line should be something similar to:

$shopProduct->reference = bin2hex(random_bytes(10));

The above will generate a random string of 10 characters, thus making sure the reference will not collide with the old one.

 

Screenshot 2025-06-08 101528.png

Link to comment
Share on other sites

Andrei thank you so much.

Your solution works fine without UNIQUE in  the 'reference' column in ps_product table but if the 'reference' column has UNIQUE index when trying to Duplicate product I receive this error:

An unexpected error occurred. [PrestaShop\PrestaShop\Core\Exception\CoreException code 0]

Snapshot_2025-06-08_093435.png

Link to comment
Share on other sites

Posted (edited)

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.

Screenshot 2025-06-08 143029.png

Screenshot 2025-06-08 143049.png

Edited by Andrei H (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...