forked from glauberportella/osc2ps-product-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
categorize-web.php
170 lines (151 loc) · 5.41 KB
/
categorize-web.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
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Glauber Portella
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Prestashop Config requirement
require_once dirname(__FILE__).'/../config/config.inc.php';
$csvCategories = dirname(__FILE__).'/categories.csv';
$firstLineIsHeader = true;
$delimiter = ';';
$enclosure = '"';
$escape = "\\";
define('COL_CATEGORY_ID', 0);
define('COL_PRODUCT_REF', 1);
function normalizeProductReference($ref, $padChar = '0', $padLen = 5, $padPos = STR_PAD_LEFT)
{
if (empty($ref))
return '';
$normalized = str_pad($ref, $padLen, $padChar, $padPos);
return $normalized;
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Importar Produtos</title>
<!-- Bootstrap CSS -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="page-header">
<h3 class="text-center">Categorize From OSCommerce to Prestashop</h3>
</div>
<?php
$fp = fopen($csvCategories, 'r');
if (!$fp)
die("<div class=\"alert alert-danger\">ERROR >> Error when try to open CSV to categorize.</div>");
// Create export array
$import = array();
$categoryNotExistData = array();
echo "<h4>1. READING CATEGORIES DATA...</h4>";
while (($data = fgetcsv($fp, 0, $delimiter, $enclosure, $escape)) !== false) {
if ($firstLineIsHeader) {
$firstLineIsHeader = false;
continue;
}
// threats NULL column value
foreach ($data as $k => &$value) {
if ($value == 'NULL' || $value == 'null') {
$value = null;
}
}
if (empty($data[COL_CATEGORY_ID])) {
continue;
}
// verify if category exists
$category = new Category((int)$data[COL_CATEGORY_ID]);
if (!Validate::isLoadedObject($category)) {
$categoryNotExistData[] = $data;
continue;
}
$import[$data[COL_CATEGORY_ID]][] = normalizeProductReference($data[COL_PRODUCT_REF]);
}
fclose($fp);
echo "<pre>DONE.</pre>";
// generate error 'category not exist' csv data
$fp = fopen(dirname(__FILE__).'/categorize-not-exists.csv', 'w');
if ($fp) {
echo "<pre>SAVING CATEGORY NOT EXIST CSV FOR RETRY...</pre>";
foreach ($categoryNotExistData as $data) {
fputcsv($fp, $data, ';', '"');
}
echo "<pre>DONE.</pre>";
fclose($fp);
}
echo "<h4>2. CATEGORIZING PRODUCTS...</h4>";
$errors = array();
$errorCsvData = array();
foreach ($import as $catId => $productRefs) {
echo "<pre><strong>CAT #$catId:</strong></pre>";
foreach ($productRefs as $ref) {
echo "<pre> [$ref]</pre>";
// get product by code ref
$sql = sprintf('SELECT id_product FROM '._DB_PREFIX_.'product WHERE SUBSTR(reference, 1, 5) = "%s"', $ref);
$product_id = Db::getInstance()->getValue($sql);
if (empty($product_id)) {
$errorCsvData[] = array($catId, $ref);
continue;
}
$product = new Product($product_id);
// associate to category
$category = new Category($catId);
if (!Validate::isLoadedObject($category)) {
$errorCsvData[] = array($catId, $ref);
continue;
}
if (false === $product->addToCategories($catId)) {
$errorCsvData[] = array($catId, $ref);
continue;
}
}
echo "<pre>DONE.</pre>";
}
// generate error csv data
$fp = fopen(dirname(__FILE__).'/categorize-errors.csv', 'w');
if ($fp) {
echo "<pre>SAVING ERROR CSV FOR RETRY...</pre>";
foreach ($errorCsvData as $data) {
fputcsv($fp, $data, ';', '"');
}
echo "<pre>DONE.</pre>";
fclose($fp);
}
echo "<p><strong>ALL DONE.</strong></p>";
?>
</div>
<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<!-- Bootstrap JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>