如何在magento 2中添加同名产品?

时间:2018-03-22 10:36:44

标签: magento magento2 magento2.2

我在Magento2上以编程方式上传产品我有不同SKU的同名产品,但是当我运行脚本Magento 2因为Url Key而出错:

Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'reine-de-naples-jour-nuit-8998.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID

我的脚本是我们用来以编程方式保存产品的脚本

<?php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager1 = Magento\Framework\App\ObjectManager::getInstance();
$directoryList = $objectManager1->get('\Magento\Framework\App\Filesystem\DirectoryList');
$path = $directoryList->getPath('media');
//var_dump($path); die;



$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$myarray = glob("Book2.csv"); 
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
if(count($myarray)){
    /*This will create an array of associative arrays with the first row column headers as the keys.*/
    $csv_map = array_map('str_getcsv', file($myarray[count($myarray)-1]));
    array_walk($csv_map, function(&$a) use ($csv_map) {
      $a = array_combine($csv_map[0], $a);
    });
    array_shift($csv_map); # remove column header
    /*End*/

    $message = '';
    $count   = 1;
    foreach($csv_map as $data){ 
        //echo '<pre>';print_r($data);exit;

$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->setName(trim($data['Name']));
$product->setTypeId('simple');
$product->setAttributeSetId(4);
$product->setSku(trim($data['model_no']));
$product->setURL(trim($data['Name']).trim($data['model_no']));
$product->setWebsiteIds(array(1));
$product->setVisibility(4);
$product->setCreatedAt(strtotime('now'));
$product->setPrice(trim($data['price']));
//$_product->setShortDescription(trim($data['Short Description'])); // add text attribute
//$_product->setDescription(trim($data['Long Description'])); // add text attribute
 $img_url = trim($data['img_big']);

                //$lastWord = substr($img_url, strrpos($img_url, '/') + 1);

                //copy($img_url, 'pub/media/product/');
                $dir = $directoryList->getPath('media').'/big/';
                $imgpath = $dir.$img_url;
                //echo $imgpath; die;
                /*$_product->addImageToMediaGallery($imgpath, array('image','thumbnail','small_image'), false, false); */
                $product->addImageToMediaGallery($imgpath, array('image', 'small_image', 'thumbnail'), false, false);
//$_product->setImage($imgpath);
//$_product->setSmallImage($imgpath);
//$_product->setThumbnail($imgpath);
$product->setStockData(array(
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        'manage_stock' => 1, //manage stock
        'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
        'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
        'is_in_stock' => 1, //Stock Availability
        'qty' => 100 //qty
        )
    );

$product->save();

    }
echo'success';
    }
?>

请建议如何将Url密钥添加到脚本我的脚本工作正常,没有相同的名称

2 个答案:

答案 0 :(得分:2)

您是否尝试省略该字段,因此Magento会自行生成 url_key

如果您希望 model_no 在网址中(由于SEO要求,我猜)你最好将它添加到名称,这对SEO来说可能更好

$product->setName(trim($data['Name']) . trim($data['model_no']));

随时加入https://magento.stackexchange.com/questions&amp;发布有关您想要的更多详情

答案 1 :(得分:0)

您正在使用set on错误的属性,即setUrl(),而应使用setUrlKey(),并且由于您没有设置不同的url密钥,因此magento尝试使用名称来生成url密钥,并且您拥有相同的属性多个产品的名称,因此最终试图为多个产品保存相同的密钥,这会给您此错误。