-
Notifications
You must be signed in to change notification settings - Fork 20
/
prepare_products.php
227 lines (183 loc) · 6.38 KB
/
prepare_products.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
require_once 'bootstrap.php';
$writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');
/*
|--------------------------------------------------------------------------
| Product Delete script
|--------------------------------------------------------------------------
*/
//$writeConnection->query("DELETE FROM `catalog_product_entity`");
//$writeConnection->query("TRUNCATE TABLE `catalog_product_entity`");
/*
|--------------------------------------------------------------------------
| Prepare for Product Import ( works with magmi)
|--------------------------------------------------------------------------
*/
echo "Creating Products files. \n";
if (!is_dir(MAGENTO.'/var/split/unformated')) {
mkdir(MAGENTO.'/var/split/unformated', 0777, true);
}
if (!is_dir(MAGENTO.'/var/split/formated')) {
mkdir(MAGENTO.'/var/split/formated', 0777, true);
}
/*
|--------------------------------------------------------------------------
| Slpitting the Product
|--------------------------------------------------------------------------
*/
$handle = fopen(MAGENTO . "/var/product.csv", 'r');
$maxRange = 500;
$count = 0;
$prefix = 1;
$line = 1;
while (($data = fgetcsv($handle)) !== false) {
if ($line == 1) {
$headers = $data;
}
if ($data[0] != null) {
$count++;
if ($count > $maxRange) {
$prefix++;
$count = 0;
if ($prefix != 1) {
$filename = MAGENTO.'/var/split/unformated/product-' . $prefix . '.csv';
$write = fopen($filename, 'a');
fputcsv($write, $headers);
fclose($write);
}
echo "Count prefix [" . $prefix . "]\n";
}
$filename = MAGENTO.'/var/split/unformated/product-'. $prefix . '.csv';
$write = fopen($filename, 'a');
fputcsv($write, $data);
fclose($write);
} else {
$filename = MAGENTO.'/var/split/unformated/product-' . $prefix . '.csv';
$write = fopen($filename, 'a');
fputcsv($write, $data);
fclose($write);
}
$line++;
}
echo "\nMEMORY USED : ".convert(memory_get_usage(true)) . "\n\n";
unset($handle, $write, $data);
//$writeConnection->query('TRUNCATE log_url; TRUNCATE log_url_info; TRUNCATE log_visitor; TRUNCATE log_visitor_info; TRUNCATE dataflow_batch_import; TRUNCATE dataflow_batch_export; TRUNCATE index_event; TRUNCATE report_event;');
function array_equal($a, $b)
{
return (is_array($a) && is_array($b) && array_diff($a, $b) === array_diff($b, $a));
}
for ($i=1; $i <=$prefix ; $i++) {
$fileName = 'product-'.$i.'.csv';
$importer = new PrepareProductImporter();
$importer->run($fileName);
unset($importer);
}
echo "\nMEMORY USED : ".convert(memory_get_usage(true)) . "\n\n";
class PrepareProductImporter
{
private $_storeIds = array();
private $_groups = array();
private $_entityTypeId;
private $_missedProduct = array();
public function __construct()
{
$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val) {
$this->_storeIds[] = Mage::app()->getStore($_eachStoreId)->getId();
}
$this->_entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
}
public static function checkEmpty($value)
{
if ($value === '') {
return false;
}
return true;
}
public function run($name)
{
$fileName = MAGENTO . '/var/split/unformated/'.$name;
echo "Reading $fileName.\n";
$file = fopen($fileName, "r");
while (!feof($file)) {
$csv[] = fgetcsv($file, 0, ',');
}
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
$csv[$i] = $csv[$i];//array_filter($csv[$i],'self::checkEmpty');
}
$i = 0;
$currentSet = null;
foreach ($csv as $row) {
$row['sku_type'] = 1;
$row['price_type'] = 1;
$row['weight_type'] = 1;
// Add Convertions Based on you needs
$row = $this->removeInvalidData($row); // Remove in valid data
//$row = $this->convertBool($row); // Example
if (trim($row['sku']) != '') {
$currentSet = $row['sku'];
$this->_groups[$currentSet][] = $row;
$i++;
} else {
$processedChild = array_filter($row, 'self::checkEmpty');
if (!empty($processedChild)) {
$newExtRow = array_merge($this->_groups[$currentSet][0], $processedChild);
if (array_equal($this->_groups[$currentSet][0], $newExtRow)) {
continue;
}
$newExtRow['bundle_skus'] = '';
$newExtRow['bundle_options'] = '';
$this->_groups[$currentSet][] = $newExtRow;
}
unset($processedChild, $newExtRow);
}
}
echo "MEMORY USED : ".convert(memory_get_usage(true)) . "\n";
fclose($file);
unset($csv, $keys, $file, $row);
$finalArray = array();
foreach ($this->_groups as $sku => $_product) {
foreach ($_product as $__product) {
$finalArray[] = $__product;
}
}
unset($this->_groups);
writeCsv($finalArray, MAGENTO . '/var/split/formated/'.$name, ',');
echo "MEMORY USED : ".convert(memory_get_usage(true)) . "\n";
}
/**
* To convert string into boll value (1 or 0)
*
* @param Array $row
* @return Array
*/
protected function convertBool($row)
{
/*
if($row['cpsp_enable'] == 'Yes') {
$row['cpsp_enable'] = 1;
} else {
$row['cpsp_enable'] = 0;
}
*/
return $row;
}
/**
* To remove invalid data form the row
*
* @param Array $row
* @return Array
*/
protected function removeInvalidData($row)
{
// Add the row that needs to removed
$invalid = array(
);
foreach ($invalid as $_invlaid) {
unset($row[$_invlaid]);
}
return $row;
}
}