English 中文(简体)
• 如何在magento添加相同的产品 2 ?
原标题:How to add same name product in magento 2?

在Magento2,我与不同的SKU有相同的名称产品,但当我使用Magento 2号文字时,由于Url Key,出现了错误:

<编码> Uncault PDOException:STATE[23,000]:廉正制约因素:1062 Duplicate entry reine-de-naples-jour-nuit-8998.html-1 for key URL_REITE_RE RequestST_PATH_STORE_ID

我的文字是我们用于在方案上拯救产品的内容。

<?php
use MagentoFrameworkAppBootstrap;
include( app/bootstrap.php );
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager1 = MagentoFrameworkAppObjectManager::getInstance();
$directoryList = $objectManager1->get( MagentoFrameworkAppFilesystemDirectoryList );
$path = $directoryList->getPath( media );
//var_dump($path); die;



$state = $objectManager->get( MagentoFrameworkAppState );
$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( MagentoCatalogModelProduct );
$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 ;
    }
?>

请建议如何用乌尔尔语补充说明我的文字,在不使用同一名称的情况下罚款。

问题回答

Have you tried omitting that field, so Magento generates the url_key itself?

如果您希望model_no >在座(由于对地静止轨道的要求,我敢说),你最好在名称上添加这个词,对地静止轨道来说,这甚至更好。

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

Feel free to join https://magento.stackexchange.com/questions & post more details about what you want

你正在使用关于错误属性(即setUrl(>)的标签,但你应当使用setUrlKey(<>),因为你没有确定不同的旋转钥匙,因此,Magento试图使用名称来生产高温钥匙,因为你对多种产品有相同的名称,从而最终试图为多种产品节省同样的钥匙,而这些产品正给你造成这种错误。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签