cancel
Showing results for 
Search instead for 
Did you mean: 

"URL key for specified store already exists." Product import

"URL key for specified store already exists." Product import

Hi, I am new Magento user and realy Magento ignoramus.

I tried import products from xml file (about 10000 products) I created a module  and run module:enable and setup:upgrade and after some time got message "URL Key for specified store already exists" ans stop working. It was imported about 2000 products. I have found some products has the same name and because the same url key. How to solve it? How can I add id or something to url key programmatically? How I can catch this error (product with duplicate url key) and change it? Here is my InstallData.php code:

<?php

namespace Test\Sample\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {
/**
* @var string
*/
protected $productType = \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE;

/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $productFactory;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/** @var \Magento\Framework\App\State **/
private $_state;

public function __construct(
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\State $state
) {
$this->productFactory = $productFactory;
$this->storeManager = $storeManager;
$this->_state = $state;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$xmlUrl = "/home/ubuntu/bcg-products.xml";
$xmlDoc = simplexml_load_file($xmlUrl);
$i = 0;
foreach($xmlDoc->product as $product) {
$i++;
$sku = (string)$product->sku;
$price = (string)$product->price;
settype($price, "float");
$special_price = (string)$product->special_price;
settype($special_price, "float");
$fix = (string)$product->fix;
settype($fix, "integer");
$akc = (string)$product->akc;
settype($akc, "integer");
$categoryid = (string)$product->categoryid;
settype($categoryid, "integer");
$category = (string)$product->category;
$brand = (string)$product->brand;
$name = (string)$product->name;
$atribute = (string)$product->attributes->atribute;
$value = (string)$product->attributes->value;
$img_url = (string)$product->images->img_url;
$desc = (string)$product->desc;

$data = [
'name' => $name,
'sku' => $sku,
'price' => $price,
'special_price' => $special_price,
'fix' => $fix,
'akc' => $akc,
'categoryid' => $categoryid,
'category' => $category,
'brand' => $brand,
'atribute' => $atribute,
'value' => $value,
'description' => $desc
];

$attributeSetId = 4; //Attribute set default
$product = $this->productFactory->create();
$product->setData($data);
$product->setTypeId($this->productType)
->setAttributeSetId($attributeSetId)
->setWebsiteIds([$this->storeManager->getDefaultStoreView()->getWebsiteId()])
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status:Smiley FrustratedTATUS_ENABLED)
->setStockData(['is_in_stock' => 1, 'manage_stock' => 0, 'qty' => 100])
->setStoreId(\Magento\Store\Model\Store:Smiley Very HappyEFAULT_STORE_ID);
if (empty($data['visibility'])) {
$product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
}
// $product->addImageToMediaGallery($img_url, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();
}
}
}

?>

Also I can not import image because I commented it.

I run Magento CE 2.2.6, on Ubuntu 17.10 server Oracle VM VirtualBox guest on Ubuntu 18.04 host.

3 REPLIES 3

Re: "URL key for specified store already exists." Product import

Hi @ignoramus

 

I understand the problem you are facing !

 

here i am sharing the link , this is workaround for this url key existing issue on product import , kindly follow the same it will resolved your issue - https://www.human-element.com/url-key-specified-store-already-exists-magento-2/

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: "URL key for specified store already exists." Product import

I am trying to import products from an XML file, but I am getting the following error:URL key for specified store already exists. This can happen if you have already imported products from a different XML file into the same store.The product import feature is not available for new stores. The product import feature was only available for existing stores (those that had been open for at least 30 days). If you have an existing store and want to import products, please contact our support team at [email protected] I will read https://augustafreepress.com/virginia-immigrants-impact-on-the-economy/ article in order to know the Impact of Virginia immigrants on the economy.

Re: "URL key for specified store already exists." Product import

Hello @ignoramus,

 

You can just use this:

$urlkey = preg_replace('#[^0-9a-z]+#i', '-', $name).'-'.preg_replace('#[^0-9a-z]+#i', '-', $sku);
$url = strtolower($urlkey);
$product->setUrlKey($url);
it will create unique url-key for your product.
 
 
I hope it will help you!.