Magento – Getting “url key for specified store already exists” error in magento2

magento-2.1multistoreurl-key

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?

Best Answer

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

Related Topic