-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.php
301 lines (234 loc) · 9.23 KB
/
connector.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/*
treba dohvatiti proizvode, napraviti csv i onda importati csv u magento
*/
$data = array("user" => "[email protected]", "password" => "pass");
$post = json_encode($data);
// getLogin
// First we need to login, and request token to access all methods
$url = "http://drop.novaengel.com/api/login";
$opts = array('http'=>
array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $post
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
// Decodes JSON into a PHP token array
$token_array = json_decode($result, true);
$token = $token_array["Token"];
// getProducts:
$api_url = 'http://drop.novaengel.com/api/products/availables/';
$lang = '/en';
$getProducts = $api_url . $token . $lang;
$request_options = array('http'=>
array(
'method' => 'GET',
'header' => 'Content-type: application/json'
)
);
$context = stream_context_create($request_options);
$products = file_get_contents($getProducts, false, $context);
// Decodes JSON into a PHP array
$products_array = json_decode($products, true);
if (!file_exists('downloaded')) {
mkdir('downloaded', 0777, true);
}
//print all data about product:
//print_r($products_array['0']);
//print all families:
//print_r($products_array['0']['Families']);
//print only the first family: (should echo Perfume)
//print_r($products_array['0']['Families']['0']);
//Prepare CSV:
$fp = fopen('./csv/data.csv', 'w') or die("Unable to open file!");
//loop all products
foreach ($products_array as $key1 => $value1) {
//loop all families to find products with Family "Perfume"
foreach ($products_array[$key1]['Families'] as $AAA => $value) {
//if product has family "Perfume"... (3933 total products with that family)
if ($value == 'Perfume') {
//print_r($products_array[$key1]['Id']);
$productId = $products_array[$key1]['Id'];
$productDescription = $products_array[$key1]['Description'];
$productSetContent = $products_array[$key1]['SetContent'];
$productPrice = $products_array[$key1]['Price'];
$productPVR = $products_array[$key1]['PVR'];
$productStock = $products_array[$key1]['Stock'];
$productBrandId = $products_array[$key1]['BrandId'];
$productBrandName = $products_array[$key1]['BrandName'];
$productGender = $products_array[$key1]['Gender'];
$row = array();
array_push($row, $productId, $productDescription, $productPrice, $productStock);
fputcsv($fp, $row);
/* $product_image = 'http://drop.novaengel.com/api/products/image/' . $token . '/' . $productId;
$options = array('http' =>
array(
'method' => 'GET',
'Content-type: application/json'
)
);
$context = stream_context_create($options);
$urlImage = file_get_contents($product_image, false, $context);
$urlImage = substr_replace($urlImage, "", -1);
$urlImage = substr($urlImage, 1);
$fp = fopen('./downloaded/' . $productId . '.jpg',"w");
$c = curl_init($urlImage);
curl_setopt($c, CURLOPT_FILE, $fp);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_POST, false);
curl_setopt($c, CURLOPT_BINARYTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
$rawdata = curl_exec($c);
fwrite($fp, $rawdata);
fclose($fp);*/
}
}
}
/*
// function defination to convert array to xml
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_numeric($key) ){
$key = 'product_'.$key; //dealing with <0/>..<n/> issues
}
if( is_array($value) ) {
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
// initializing or creating array
$data = array($products_array);
// creating object of SimpleXMLElement
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><products></products>');
// function call to convert array to xml
array_to_xml($data,$xml_data);
//create dir for xml if not exists
if (!file_exists('xml')) {
mkdir('xml', 0755, true);
}
//saving generated xml file;
$result = $xml_data->asXML("xml/FranelaArtikli-" . date('d-m-Y') . "-" . date('H-i-s') . ".xml");
echo "xml created successful";
*/
/* BARE MINIMUM COLUMS:
sku, name, price, qty, categories, url_key, type, grouped_skus, cs_skus, us_skus, short_description, description, image, small_image, thumbnail
*/
set_time_limit(0);
ini_set('memory_limit', '1024M');
include_once "app/Mage.php";
include_once "downloader/Maged/Controller.php";
Mage::init();
$app = Mage::app('default');
//The category names should be exactly the same name from the csv file where the id is the corresponding category id in magento. This is done when the csv file doesn't contain ids for categories but the name of categories.
$categories = array(
'Category 1' => 3,
'Category 2' => 4,
'Category 3' =>5,
'Category 4'=>6,
);
$row = 0;
if (($handle = fopen("csv/data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo 'Importing product: '.$data[0].'<br />';
foreach($data as $d)
{
// echo $d.'<br />';
}
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
if($row == 1) continue;
$product = Mage::getModel('catalog/product');
$product->setSku($data[0]);
$product->setName($data[0]);
$product->setDescription($data[1]);
$product->setShortDescription('');
// $product->setManufacturer($data[0]);
$product->setPrice($data[2]);
$product->setTypeId('simple');
/*
$fullpath = 'media/catalog/product/thumb/';
$ch = curl_init ($data[0]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
$fullpath = $fullpath.$data[0].'.jpg';
if(file_exists($fullpath)) {
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
$product->addImageToMediaGallery($fullpath, 'thumbnail', false);
$fullpath = 'media/catalog/product/small/';
$ch = curl_init ($data[0]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
$fullpath = $fullpath.$data[0].'.jpg';
if(file_exists($fullpath)) {
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
$product->addImageToMediaGallery($fullpath, 'small-image', false);
$fullpath = 'media/catalog/product/high/';
$ch = curl_init ($data[0]);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
$fullpath = $fullpath.$data[13].'.jpg';
if(file_exists($fullpath)) {
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
$product->addImageToMediaGallery($fullpath, 'image', false);
*/
$product->setAttributeSetId(4); // need to look this up. 4 je default.
//$product->setCategoryIds(array($categories[$data[11]])); // need to look these up
// $product->setCategoryIds(array(2)); // need to look these up
$product->setWeight(0);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled
// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
/* $stockData = $product->getStockData();
$stockData['qty'] = $data[18]; //18
$stockData['is_in_stock'] = $data[17]=="In Stock"?1:0; //17
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
*/
$product->save();
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('stock_id', 1);
$stockItem->setData('store_id', 1);
$stockItem->setData('manage_stock', 1);
$stockItem->setData('use_config_manage_stock', 0);
$stockItem->setData('min_sale_qty', 1);
$stockItem->setData('use_config_min_sale_qty', 0);
$stockItem->setData('max_sale_qty', 1000);
$stockItem->setData('use_config_max_sale_qty', 0);
$stockItem->setData('qty', $data[3]);
$stockItem->save();
}
fclose($handle);
}
//exec("php -f /var/www/import/web/shell/indexer.php reindexall"); // reindex all (apsolute path)