Mokhtarus Posted May 7, 2022 Share Posted May 7, 2022 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 More sharing options...
dario68 Posted June 4 Share Posted June 4 same problem, any ideas? PS 8.2.0 Link to comment Share on other sites More sharing options...
Andrei H Posted June 8 Share Posted June 8 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. Link to comment Share on other sites More sharing options...
dario68 Posted June 8 Share Posted June 8 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] Link to comment Share on other sites More sharing options...
Andrei H Posted June 8 Share Posted June 8 (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. Edited June 8 by Andrei H (see edit history) Link to comment Share on other sites More sharing options...
dario68 Posted June 8 Share Posted June 8 Thank you very very much Andrei Now it works perfectly! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now