Skip to content

Commit 2d1d001

Browse files
committed
[CATEGORY] Added ImportCSV Command
1 parent 8a063e6 commit 2d1d001

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

Console/Command/Product/ImportCsv.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
namespace FireGento\FastSimpleImportDemo\Console\Command\Product;
3+
4+
use Magento\Framework\Component\ComponentRegistrar;
5+
use Magento\Framework\App\Filesystem\DirectoryList;
6+
use FireGento\FastSimpleImportDemo\Console\Command\AbstractImportCommand;
7+
use Magento\Framework\App\ObjectManagerFactory;
8+
use Magento\ImportExport\Model\Import;
9+
use League\Csv\Reader;
10+
11+
class ImportCsv extends AbstractImportCommand
12+
{
13+
const IMPORT_FILE = "importfile.csv";
14+
15+
/**
16+
* @var \Magento\Framework\Filesystem\Directory\ReadFactory
17+
*/
18+
private $readFactory;
19+
/**
20+
* @var DirectoryList
21+
*/
22+
private $directory_list;
23+
24+
25+
/**
26+
* Constructor
27+
*
28+
* @param ObjectManagerFactory $objectManagerFactory
29+
*/
30+
public function __construct(
31+
ObjectManagerFactory $objectManagerFactory,
32+
\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
33+
\Magento\Framework\App\Filesystem\DirectoryList $directory_list
34+
)
35+
{
36+
37+
parent::__construct($objectManagerFactory);
38+
39+
40+
$this->readFactory = $readFactory;
41+
42+
$this->directory_list = $directory_list;
43+
}
44+
45+
protected function configure()
46+
{
47+
48+
$this->setName('fastsimpleimportdemo:products:importcsv')
49+
->setDescription('Import Products from CSV');
50+
$this->setBehavior(Import::BEHAVIOR_ADD_UPDATE);
51+
$this->setEntityCode('catalog_product');
52+
53+
parent::configure();
54+
}
55+
56+
/**
57+
* @return array
58+
*/
59+
protected function getEntities()
60+
{
61+
$csvIterationObject = $this->readCSV();
62+
$data = array();
63+
// Do mapping here:
64+
foreach($csvIterationObject as $row){
65+
$data[] = $row;
66+
}
67+
// Mapping end
68+
//var_dump($data);
69+
//die();
70+
return $data;
71+
}
72+
73+
protected function readCSV()
74+
{
75+
$csvObj = Reader::createFromString($this->readFile(self::IMPORT_FILE));
76+
$csvObj->setDelimiter(',');
77+
$results = $csvObj->fetchAssoc();
78+
return $results;
79+
80+
}
81+
82+
protected function readFile($fileName)
83+
{
84+
$path = $this->directory_list->getRoot();
85+
$directoryRead = $this->readFactory->create($path);
86+
return $directoryRead->readFile($fileName);
87+
}
88+
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Installation Instructions with Composer(Master Branch)
4141
### Delete all products in Catalog(Warning : really all Products):
4242
`bin/magento fastsimpleimportdemo:products:deleteall`
4343

44+
### Import products from CSV File:
45+
`bin/magento fastsimpleimportdemo:products:importcsv`
4446

4547
## Customers
4648

@@ -50,6 +52,8 @@ Installation Instructions with Composer(Master Branch)
5052
### Delete all customers:
5153
`bin/magento fastsimpleimportdemo:customers:deleteall`
5254

53-
### Import Categorys(BETA,work in Progress):
55+
## Categories
56+
57+
### Import Categories:
5458
`bin/magento fastsimpleimportdemo:category:import`
5559

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"homepage": "https://github.com/magento-hackathon/FireGento_FastSimpleImport2_Demo",
55
"require": {
66
"php": "~5.5.0|~5.6.0|~7.0.0",
7-
"firegento/fastsimpleimport": "*"
7+
"firegento/fastsimpleimport": "*",
8+
"league/csv": "^8.0"
89
},
910
"type": "magento2-module",
1011
"license": "GPL-3.0",

etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<item name="import_simple" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Product\ImportSimple</item>
1818
<item name="import_bundle" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Product\ImportBundle</item>
1919
<item name="import_multiselect" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Product\ImportMultiselect</item>
20+
<item name="import_csv" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Product\ImportCsv</item>
2021

2122
<item name="import_customer" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Customer\ImportCustomer</item>
2223
<item name="import_category" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Category\ImportCategory</item>
@@ -25,6 +26,7 @@
2526
<item name="export_orders" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Orders\ExportOrders</item>
2627
<item name="export_customers" xsi:type="object">FireGento\FastSimpleImportDemo\Console\Command\Customer\ExportCustomer</item>
2728

29+
2830
</argument>
2931
</arguments>
3032
</type>

0 commit comments

Comments
 (0)