0

I have installed magento2.1 and it was working properly with multi-store.

When I have disabled some categories from a "sub" store, and try to add any product (already assigned to "sub" store) to that category (which is edited in "sub" store) system returns error url key for specified store already exists.

If I removed "sub" store from "product in website" and try to assign that category again it saved successfully.

That's mean it is the issue with categories which has different value of any attribute in multi-store view.

I found so many post regarding this, but unable to fix this issue.

How to solve this issue in magento2?

0

I did following changes and it is working.

Magento\UrlRewrite\Model\Storage\DbStorage.php file

protected function insertMultiple($data)
    {
        try {
            $keys=array();
            foreach($data as $current=>$_data)
            {
                $key = $_data['request_path'];
                if(in_array($key,$keys))
                {
                    unset($data[$current]);
                }else
                {
                    $keys[]=$key;
                }
            }

            $this->connection->insertMultiple($this->resource->getTableName(self::TABLE_NAME), $data);
        } catch (\Exception $e) {
            if ($e->getCode() === self::ERROR_CODE_DUPLICATE_ENTRY
                && preg_match('#SQLSTATE\[23000\]: [^:]+: 1062[^\d]#', $e->getMessage())
            ) {
                throw new \Magento\Framework\Exception\AlreadyExistsException(
                    __('URL key for specified store already exists.')
                );
            }
            throw $e;
        }
    }

Reference :

https://community.magento.com/t5/Technical-Issues/quot-URL-key-for-specified-store-already-exists-quot-cannot-save/td-p/67837

| improve this answer | |
0

If you upgraded an existing Magento 1 store, there are a couple columns in the product entity table which may contain data that can contribute to this. We migrated from Magento 1 and ran into this issue when updating / moving products in a category.

What I ended up doing was making sure I knew for sure that there were no duplicate values within a store for each url_key value. If I was sure there were no duplicates, I then deleted the value for the attribute url_path for products. After removing that data, I had to save the categories again to regenerate some rules, but things worked. We haven't had an issue with it since.

| improve this answer | |
0

I have fixed this issue in more than 3-4 projects.

If you want to resolve this issue, you can upgrade your version with latest one or try below code logic:

Try this solution by overriding below function in your custom module :)

Path: vendor/magento/module-url-rewrite/Model/Storage

protected function doReplace(array $urls){ 
    $this->deleteOldUrls($urls); 
    $data = [];
    $storeId_requestPaths = [];
    foreach ($urls as $url) {
        $storeId = $url->getStoreId();
        $requestPath = $url->getRequestPath();
        $sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
        $exists = $this->connection->fetchOne($sql); 
        if ($exists) continue;
        $storeId_requestPaths[] = $storeId . '-' . $requestPath;
        $data[] = $url->toArray();
    }

    // Remove duplication data;
    $n = count($storeId_requestPaths);
    for ($i = 0; $i < $n - 1; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
                unset($data[$j]);
            }
        }
    }
    try {
        $this->insertMultiple($data);
    } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
        /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
        $urlConflicted = [];
        foreach ($urls as $url) {
            $urlFound = $this->doFindOneByData(
                [
                    UrlRewriteData::REQUEST_PATH => $url->getRequestPath(),
                    UrlRewriteData::STORE_ID => $url->getStoreId(),
                ]
            );
            if (isset($urlFound[UrlRewriteData::URL_REWRITE_ID])) {
                $urlConflicted[$urlFound[UrlRewriteData::URL_REWRITE_ID]] = $url->toArray();
            }
        }
        if ($urlConflicted) {
            throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
                __('URL key for specified store already exists.'),
                $e,
                $e->getCode(),
                $urlConflicted
            );
        } else {
            throw $e->getPrevious() ?: $e;
        }
    }

    return $urls;
}

Happy Coding :)

Thanks,

Ketan T.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.