I am facing this issue while saving a product programmatically on Magento 2.2.5
In any module, if I do $product->save();
OR $this->productRepository->save($product);
inside a loop for multiple products. I get:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘the-lipstick.html-1’ for key ‘URL_REWRITE_REQUEST_PATH_STORE_ID’ in /home/dev3/www/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
The error is similar to the one described here : https://www.human-element.com/url-key-specified-store-already-exists-magento-2/
Products save fine with admin area login.
Any suggested fixes so far including the ones modifying the core files (DBStorage.php) do not work on 2.2.5.
What I tried so far:
1. Fix from https://www.human-element.com/url-key-specified-store-already-exists-magento-2/
2. Fix from https://magento.stackexchange.com/questions/210359/magento-2-product-url-rewrite-issue
Please suggest a solution/fix for M 2.2.5
3
Answers
My Fix : In di.xml -
<preference for="MagentoUrlRewriteModelStorageDbStorage" type="MyCompanyFixUrlsModelProductUrlFix" />
In ProductFixUrl write these two functions :
After a migration and a week digging into the problem the only thing that worked for me was https://www.safemage.com/url-optimization-after-migration-magento-2.html
I had to downgrade to 2.2.7 to use it. It says it works on 2.3 but it does not.
After looking on internet for days i can’t find exact solution of this.
Then i found if we change the URLKEY of the category it will not show this error so i have done this.
I use random function to add category in database with random no using ->setUrlKey(rand(1,1000000000)); you can add any thing in this like duplicate category name with some random no etc.
and errors gone if it helps you give an UP. thanks