3

Sometimes during import products we receive issue: The URL key for the selected store already exists.

We have found one website with solution:

https://www.human-element.com/url-key-specified-store-already-exists-magento-2/

but I see this path: not be compatible with versions of Magento 2.1.8 and above

our version magento is 2.2.5

Can anyone check it for us if this solution will work on our version?

@update

We try install this path but during setup:di:compile issue:

 [Exception]
  Warning: Declaration of HE\UrlkeyRewrite\Model\ProductUrlRewriteGenerator::
  generateForGlobalScope($productCategories) should be compatible with Magent
  o\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::generateForGlobalScop
  e($productCategories, $product = NULL, $rootCategoryId = NULL) in /home/user/domains/website.com/public_html/app/code/HE/UrlkeyRewrite/Model/ProductU
  rlRewriteGenerator.php on line 50

code producturlrewritegenertor.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace HE\UrlkeyRewrite\Model;

use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator;
use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
use Magento\Store\Model\Store;
use Magento\Catalog\Model\Product\Visibility;

class ProductUrlRewriteGenerator extends \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator
{

    /**
     * Generate list of urls for global scope
     *
     * Update adds in a clone of the collection and setting the storeId
     * Variation of this post:
     * https://magento.stackexchange.com/questions/169352/magento-2-url-key-for-specified-store-already-exists
     *
     * @param \Magento\Framework\Data\Collection $productCategories
     * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
     */
    protected function generateForGlobalScope($productCategories)
    {
        $urls = [];
        $productId = $this->product->getEntityId();
        foreach ($this->product->getStoreIds() as $id) {
            if (!$this->isGlobalScope($id)
                && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $productId, Product::ENTITY)
            ) {
                // Default: $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $productCategories));
                // before loading the category collection by looping it, clone it and set the correct store id,
                // so we get the correct url_path & url_key for that specific store id
                $storeSpecificProductCategories = clone $productCategories;
                $storeSpecificProductCategories->setStoreId($id);

                $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $storeSpecificProductCategories));
            }
        }
        return $urls;
    }

}

Your Answer

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

Browse other questions tagged or ask your own question.