简体   繁体   中英

How to create a simple product programmatically using csv data in magento2

I am trying to create a simple product programmatically using csv data. But my product doest not insert in db and admin panel??? Any suggestion pls

Sku,Name,AttributeSetId,CategoryId,Description,Status,TypeId,Price,WebsiteIds,Visibility,UrlKey //title for the csv data
nailPolish,nailpolish,9,2,verybrightproduct,1,simple,200,base,4,nailpolish//csv data
nailRemover,nailremover,9,2,niceproduct,1,simple,200,base,4,nailremover//csv data
<?php

use Magento\Framework\App\Bootstrap;// add bootstrap

include("app/bootstrap.php");
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


function readOptions() //to read csv file
{
    $options = array();
    $file = 'csv/csvSimpleproduct.csv';
    $handle = fopen($file, "r");
    $headers = false;
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers)
                $headers[] = $data;
            else
                $options[] = $data;
        }
        fclose($handle);

    }
    return $options;
}

$importProduct = readOptions();
echo '<pre>';
print_r($importProduct);//printing array values 
echo '</pre>';



foreach ($importProduct as $importProducts) { //to get single array values


    $product = $objectManager->create('\Magento\Catalog\Model\Product');//creating object manager
    $product->setSku($importProducts[0]);//to set sku values
    $product->setName($importProducts[1]);// to set name of the product
    $product->setAttributeSetId($importProducts[2]); //Default attribute set for products
    $product->setCategories($importProducts[3]);//
    $product->setDescription($importProducts[4]);//to give descriptions
    $product->setStatus($importProducts[5]);
    $product->setProductType($importProducts[6]);
    $product->setPrice($importProducts[7]);// to give price of an product
    $product->setWebsiteIds(array(1));//set main website view
    $product->setVisibility($importProducts[9]);
    $product->setUrlKey($importProducts[10]);//to set url to the product

}

my csv file is:

Sku,Name,AttributeSetId,CategoryId,Description,Status,TypeId,Price,WebsiteIds,Visibility,UrlKey 
nailPolish,nailpolish,9,2,verybrightproduct,1,simple,200,base,4,nailpolish
nailRemover,nailremover,9,2,niceproduct,1,simple,200,base,4,nailremover

Maybe something else is wrong, but you need to save your product by product repository

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */


$product = $productRepository->save($product);

for now, you do not save product at all

also, it's not a best practice to use object manager directly, use product factory for creating new objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM