Import magento 2 products programmatically

vinaymaurya134

New member
XNullUser
Joined
May 20, 2022
Messages
2
Reaction score
0
Points
1
NullCash
16
As the first create the importproducts.php file inside the script folder in the magento root directory.Then establish the database connection as shown below.

  1. <?php
  2. define('DS', DIRECTORY_SEPARATOR);
  3. use \Magento\Framework\App\Bootstrap;
  4. include('../app/bootstrap.php');
  5. $bootstrap = Bootstrap::create(BP, $_SERVER);
  6. $objectManager = $bootstrap->getObjectManager();
  7. $app_state = $objectManager->get('\Magento\Framework\App\State');
  8. $app_state->setAreaCode('frontend');
  9. $productsData = getProducts();
  10. importSimpleProducts( $productsData , $objectManager );

Read CSV​

This step reads the products data from the CSV.

  1. function getProducts()
  2. {
  3. $file = 'csv/products.csv';
  4. $arrResult = array();
  5. $headers = false;
  6. $handle = fopen($file, "r");
  7. if (empty($handle) === false) {
  8. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  9. if (!$headers) {
  10. $headers[] = $data;
  11. } else {
  12. $arrResult[] = $data;
  13. }
  14. }
  15. fclose($handle);
  16. }
  17. return $arrResult;
  18. }

Import magento 2 products​

The above function reads the data from the CSV and return the data in an array. The following code iterate through the array and create the products as show above.

  1. /*
  2. * Import Simple Products
  3. */
  4. function importSimpleProducts( $importProducts, $objectManager ) {
  5. // Path to media folder
  6. $filesystem = $objectManager->create('Magento\Framework\Filesystem');
  7. $mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
  8. $mediaPath = $mediaDirectory->getAbsolutePath();
  9. foreach( $importProducts as $importProduct ) {
  10. try {
  11. $product = $objectManager->create('\Magento\Catalog\Model\Product');
  12. $product->setWebsiteIds(array(1));
  13. $product->setAttributeSetId(4);
  14. $product->setTypeId('simple');
  15. $product->setCreatedAt(strtotime('now'));
  16. $product->setName($importProduct[1]);
  17. $product->setSku($importProduct[3]);
  18. $product->setWeight($importProduct[16]);
  19. $product->setStatus(1);
  20. $category_id= array(30,24);
  21. $product->setCategoryIds($category_id);
  22. $product->setTaxClassId(0); // (0 - none, 1 - default, 2 - taxable, 4 - shipping)
  23. $product->setVisibility(4); // catalog and search visibility
  24. $product->setColor(24);
  25. $product->setPrice($importProduct[11]) ;
  26. $product->setCost(1);
  27. $product->setMetaTitle($importProduct[1]);
  28. $product->setMetaKeyword($importProduct[26]);
  29. $product->setMetaDescription($importProduct[28]);
  30. $product->setDescription($importProduct[27]);
  31. $product->setShortDescription($importProduct[27]);
  32. $product->setStockData(
  33. array(
  34. 'use_config_manage_stock' => 0,
  35. 'manage_stock' => 1, // manage stock
  36. 'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed
  37. 'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
  38. 'is_in_stock' => 1, // Stock Availability of product
  39. 'qty' => (int)$importProduct[6]
  40. )
  41. );
  42. $product->save();
  43. echo "Upload simple product id :: ".$product->getId()."\n";
  44. }
  45. catch(Exception $e)
  46. {
  47. echo 'Something failed for product import ' . $importProduct[1] . PHP_EOL;
  48. print_r($e);
  49. }
  50. }
  51. }
 

zyhivan

Member
XNullUser
Joined
Aug 6, 2021
Messages
790
Reaction score
0
Points
16
NullCash
3,688
great module for import! Thank you so much! Thank you so much!
 

greg2

New member
XNullUser
Joined
Jul 26, 2022
Messages
3
Reaction score
0
Points
1
NullCash
4
Thank you for sharing your code !
 

ececec

New member
XNullUser
Joined
Dec 9, 2021
Messages
8
Reaction score
0
Points
1
NullCash
7
Thank you for sharing!
really helped me🤗 so good
 
Top