Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 615907b

Browse files
author
Tyler Mills
committed
Product import functionality updated
1 parent c3020b1 commit 615907b

File tree

3 files changed

+125
-3
lines changed

3 files changed

+125
-3
lines changed

n98-magerun.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ commands:
55
customCommands:
66
- Etre\Shell\Console\Commands\Patch\ListCommand
77
- Etre\Shell\Console\Commands\Patch\DebugCommand
8-
- Etre\Shell\Console\Commands\Import\ProductsCommand
8+
- Etre\Shell\Console\Commands\Import\ProductsCommand
9+
- Etre\Shell\Console\Commands\Import\ListCommand
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Etre\Shell\Console\Commands\Import;
4+
5+
use Etre\Shell\Console\Commands\Import\Products\MapInput;
6+
use Etre\Shell\Helper\DirectoryHelper;
7+
use Etre\Shell\Helper\PatchesHelper;
8+
use Maatwebsite\Excel\Classes\PHPExcel;
9+
use Maatwebsite\Excel\Excel;
10+
use N98\Magento\Command\AbstractMagentoCommand;
11+
use Symfony\Component\Console\Helper\ProgressBar;
12+
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Console\Question\Question;
17+
18+
class ListCommand extends AbstractMagentoCommand
19+
{
20+
const PRODUCT_IDENTIFIER = "sku";
21+
/** @var DirectoryHelper $directoryHelper */
22+
protected $directoryHelper;
23+
/** @var PatchesHelper $patchesHelper */
24+
protected $patchesHelper;
25+
26+
/**
27+
* PatchCommand constructor.
28+
* @param $patchHelper
29+
*/
30+
public function __construct($name = null)
31+
{
32+
$this->directoryHelper = new DirectoryHelper();
33+
$this->patchesHelper = new PatchesHelper($this->directoryHelper);
34+
35+
parent::__construct($name);
36+
}
37+
38+
public function configure()
39+
{
40+
$this
41+
->setName('etre:import:list')
42+
->setDescription('List files in import directory.')
43+
->addArgument("path",InputArgument::OPTIONAL, "Relative path to subdirectory within ./var/import")
44+
->setHelp("This command lists available files");
45+
}
46+
47+
protected function execute(InputInterface $input, OutputInterface $output)
48+
{
49+
$this->detectMagento($output);
50+
if(!$this->initMagento()) {
51+
return;
52+
}
53+
$helper = $this->getHelper('question');
54+
$importDirectory = \Mage::getBaseDir() . DS . "var" . DS . "import" . DS . $input->getArgument("path");
55+
$output->writeln(scandir($importDirectory));
56+
}
57+
58+
/**
59+
* @param string $importFile
60+
* @return \PHPExcel
61+
*/
62+
protected function loadFile($importFile)
63+
{
64+
try {
65+
$inputFileType = \PHPExcel_IOFactory::identify($importFile);
66+
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
67+
$objPHPExcel = $objReader->load($importFile);
68+
return $objPHPExcel;
69+
} catch(Exception $e) {
70+
die('Error loading file "' . pathinfo($importFile, PATHINFO_BASENAME) . '": ' . $e->getMessage());
71+
}
72+
return $objPHPExcel;
73+
}
74+
75+
/**
76+
* @param $lastColumn
77+
* @param $sheet
78+
* @param $row
79+
* @param $inputMappings
80+
*/
81+
protected function mapFileColumns($lastColumn, $sheet, $row, $inputMappings)
82+
{
83+
$columnReverseMapping = [];
84+
85+
for($column = 'A'; $column != $lastColumn; $column++):
86+
$cell = $sheet->getCell($column . $row);
87+
$columnLetter = $cell->getColumn();
88+
$colIndex = \PHPExcel_Cell::columnIndexFromString($columnLetter);
89+
unset($mapped_magento_attribute);
90+
91+
$columnHeaderValue = $cell->getValue();
92+
foreach($inputMappings as $attribute_code => $headerValue):
93+
if(($headerValue == $columnHeaderValue || ($headerValue == $colIndex))):
94+
$mapped_magento_attribute = $attribute_code;
95+
endif;
96+
endforeach;
97+
if(!isset($mapped_magento_attribute)):
98+
continue;
99+
else:
100+
$columnReverseMapping[$columnLetter]['magento_attribute_map'] = $mapped_magento_attribute;
101+
$columnReverseMapping[$columnLetter]["columnIndex"] = $colIndex;
102+
$columnReverseMapping[$columnLetter]["columnName"] = $columnHeaderValue;
103+
$columnReverseMapping[$columnLetter]["column"] = $columnLetter;
104+
endif;
105+
endfor;
106+
return $columnReverseMapping;
107+
}
108+
109+
/**
110+
* @param $rowMap
111+
* @return mixed
112+
*/
113+
protected function itemsToUpdateFilter($rowMap)
114+
{
115+
$attributesToUpdate = $rowMap;
116+
unset($attributesToUpdate[self::PRODUCT_IDENTIFIER]);
117+
return $attributesToUpdate;
118+
}
119+
120+
}

src/Console/Commands/Import/ProductsCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Maatwebsite\Excel\Excel;
1010
use N98\Magento\Command\AbstractMagentoCommand;
1111
use Symfony\Component\Console\Helper\ProgressBar;
12+
use Symfony\Component\Console\Input\InputArgument;
1213
use Symfony\Component\Console\Input\InputInterface;
1314
use Symfony\Component\Console\Input\InputOption;
1415
use Symfony\Component\Console\Output\OutputInterface;
@@ -39,7 +40,7 @@ public function configure()
3940
$this
4041
->setName('etre:import:products')
4142
->setDescription('Import products')
42-
->addArgument("path", null, "Path to file in var directory: <comment>import/filename.csv</comment>")
43+
->addArgument("path",InputArgument::REQUIRED, "Path to Excel compatible file within var/import directory: <comment>subdir/filename.ext</comment>")
4344
->addOption("map-attribute", "m", InputOption::VALUE_REQUIRED,
4445
"Map attribute codes to headers." .
4546
"\n<comment>-m mage_attribute_code:file_column_header,mage_attribute_code:file_column_header,...</comment>" .
@@ -62,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6263
$mapInput = new MapInput($attributeMapString);
6364
$mapInput->getMappedAttributes();
6465

65-
$importFile = \Mage::getBaseDir() . DS . "var" . DS . "import" . DS . "import-20161017070019-1_Prepped_for_import_-_artificial_attributes-libre.csv";
66+
$importFile = \Mage::getBaseDir() . DS . "var" . DS . "import" . DS . $input->getArgument("path");
6667
$excelDocument = $this->loadFile($importFile);
6768
$sheet = $excelDocument->getSheet(0);
6869
$lastColumn = $sheet->getHighestColumn();

0 commit comments

Comments
 (0)