ice_import - Version 1.2.0

Version Notes

- Import any field from CSV to any text field

Download this release

Release Info

Developer IceShop
Extension ice_import
Version 1.2.0
Comparing to
See all releases


Code changes from version 1.1.0 to 1.2.0

app/code/community/Capacitywebsolutions/Importproduct/Model/Convert/Adapter/Product.php CHANGED
@@ -12,704 +12,381 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
12
  protected $_connRes = null;
13
  protected $_tablePrefix = '';
14
 
 
 
 
 
 
 
 
 
 
 
15
  public function saveRow(array $importData) {
16
 
17
- if (!empty($importData['mpn']) && !empty($importData['brand_name']) && !empty($importData['categories'])) {
18
- // custom logic
19
- $this->_connRes = Mage::getSingleton('core/resource')->getConnection('core_write');
 
 
20
 
21
- $tablePrefix = (array)Mage::getConfig()->getTablePrefix();
22
- if (!empty($tablePrefix)) {
23
- $this->_tablePrefix = $tablePrefix[0];
24
- }
25
 
26
- $this->_coreSaveRow($importData);
27
- //$this->_capacitySaveRow($importData);
28
- } else {
29
- // old logic
30
- $this->_capacitySaveRow($importData);
31
  }
32
- }
 
33
 
34
- protected function _coreSaveRow(array $importData){
35
- if (empty($importData['store'])) {
36
  if (!is_null($this->getBatchParams('store'))) {
37
  $store = $this->getStoreById($this->getBatchParams('store'));
38
  } else {
39
  $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
40
  Mage::throwException($message);
41
  }
42
- }else {
43
- $store = $this->getStoreByCode($importData['store']);
44
  }
45
-
46
  if ($store === false) {
47
- $message = Mage::helper('catalog')->__('Skip import row, store "%s" not exists', $importData['store']);
48
  Mage::throwException($message);
49
  }
 
50
 
51
- $storeId = $store->getId();
 
 
 
 
 
52
 
53
- $websiteId = false;
54
- if (!empty($importData['websites'])) {
55
- $website = Mage::app()->getWebsite(trim($importData['websites']));
56
- $websiteId = $website->getId();
57
- } else {
58
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'website');
59
- Mage::throwException($message);
60
- }
61
 
62
- if (empty($importData['sku'])) {
 
63
  $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
64
  Mage::throwException($message);
65
  }
 
66
 
67
- // attributes set up
68
- $this->_connRes->query("
69
- SELECT @product_entity_type_id := entity_type_id FROM " . $this->_tablePrefix . "eav_entity_type WHERE entity_type_code = 'catalog_product';
70
- SELECT @category_entity_type_id := entity_type_id FROM " . $this->_tablePrefix . "eav_entity_type WHERE entity_type_code = 'catalog_category';
71
- SELECT @attribute_set_id := entity_type_id FROM " . $this->_tablePrefix . "eav_entity_type WHERE entity_type_code = 'catalog_product';
72
- SELECT @attribute_set_id := `attribute_set_id` FROM eav_attribute_set
73
- WHERE attribute_set_name = 'Default' AND entity_type_id =
74
- (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product');
75
-
76
- SELECT @name_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'name' AND entity_type_id = @product_entity_type_id;
77
- SELECT @mpn_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'mpn' AND entity_type_id = @product_entity_type_id;
78
- SELECT @brand_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'brand_name' AND entity_type_id = @product_entity_type_id;
79
- SELECT @desc_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'description' AND entity_type_id = @product_entity_type_id;
80
- SELECT @sh_desc_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'short_description' AND entity_type_id = @product_entity_type_id;
81
- SELECT @sku_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'sku' AND entity_type_id = @product_entity_type_id;
82
- SELECT @weight_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'weight' AND entity_type_id = @product_entity_type_id;
83
- SELECT @status_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'status' AND entity_type_id = @product_entity_type_id;
84
- SELECT @url_key_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'url_key' AND entity_type_id = @product_entity_type_id;
85
- SELECT @visibility_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'visibility' AND entity_type_id = @product_entity_type_id;
86
- SELECT @price_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'price' AND entity_type_id = @product_entity_type_id;
87
- SELECT @stock_id := `stock_id` FROM `" . $this->_tablePrefix . "cataloginventory_stock` where stock_name = 'Default';
88
- SELECT @delivery_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'delivery_eta' AND entity_type_id = @product_entity_type_id;
89
-
90
- SELECT @unspcs_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'unspsc' AND entity_type_id = @category_entity_type_id;
91
- SELECT @category_name_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'name' AND entity_type_id = @category_entity_type_id;
92
- SELECT @category_active_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'is_active' AND entity_type_id = @category_entity_type_id;
93
- SELECT @include_nav_bar_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = 'include_in_menu' AND entity_type_id = @category_entity_type_id;
94
- ");
95
-
96
- $prodIdFetch = $this->_connRes->fetchRow('SELECT entity_id FROM catalog_product_entity WHERE sku = :sku limit 1' , array(':sku' => $importData['sku']));
97
- $productId = $prodIdFetch['entity_id'];
98
-
99
- if (!empty($productId)) {
100
- // check import type (Import only price & qty or all product info)
101
- if('import_price_stock' == Mage::getStoreConfig('importprod_root/importprod/import_only_prices_stock',
102
- $storeId)) {
103
- $this->_corePriceStock($websiteId, $productId, $importData['price'], $importData['qty']);
104
- return true;
105
- }
106
- } else {
107
- $productId = false;
108
- }
109
 
110
- // get category id
111
- if (isset($importData['categories'])) {
112
- $categoriesToActiveConf = Mage::getStoreConfig('importprod_root/importprod/category_active',
113
- $storeId);
 
114
 
115
- if (!empty($importData['leader_categories'])) {
116
- $leader_categories = $importData['leader_categories'];
117
- $leader_store = $this->getStoreByCode($importData['leader_store']);
118
- } else {
119
- $leader_categories = '';
120
- $leader_store = '';
121
- }
122
 
123
- $unspsc = '';
124
- if (!empty($importData['unspsc'])) {
125
- $unspsc = $importData['unspsc'];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  }
127
-
128
- $unspscPath = '';
129
- if (!empty($importData['unspsc_path'])) {
130
- $unspscPath = $importData['unspsc_path'];
131
- } else {
132
- $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC not defined in store');
133
- Mage::throwException($message);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  }
135
- $categoryId = $this->_addCategories($importData['categories'], $storeId, $importData['leader_store'], $unspsc, $unspscPath, $categoriesToActiveConf);
136
- }
137
-
138
- // agregate product data
139
- $productData = array();
140
- $productData['websiteId'] = $websiteId;
141
- $productData['storeId'] = $storeId;
142
- $productData['name'] = $importData['name'];
143
- $productData['sku'] = $importData['sku'];
144
- $productData['mpn'] = $importData['mpn'];
145
- $productData['brand_name'] = $importData['brand_name'];
146
- $productData['short_description'] = $importData['short_description'];
147
- $productData['description'] = $importData['description'];
148
- $productData['store'] = $importData['store'];
149
- $productData['price'] = $importData['price'];
150
- $productData['qty'] = $importData['qty'];
151
- $productData['weight'] = $importData['weight'];
152
- $productData['status'] = $importData['status'];
153
- $productData['visibility'] = $importData['visibility'];
154
- $productData['is_in_stock'] = $importData['is_in_stock'];
155
- $productData['delivery_eta'] = $importData['delivery_eta'];
156
- $productData['productId'] = $productId;
157
- $productData['categoryId'] = $categoryId;
158
-
159
- return (bool)$this->_coreSave($productData);
160
- }
161
 
162
- protected function _capacitySaveRow(array $importData){
163
- $product = $this->getProductModel()
164
- ->reset();
165
-
166
- if (empty($importData['store'])) {
167
- if (!is_null($this->getBatchParams('store'))) {
168
- $store = $this->getStoreById($this->getBatchParams('store'));
169
- } else {
170
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
171
- Mage::throwException($message);
172
- }
173
- }
174
- else {
175
- $store = $this->getStoreByCode($importData['store']);
176
- }
177
-
178
- if ($store === false) {
179
- $message = Mage::helper('catalog')->__('Skip import row, store "%s" not exists', $importData['store']);
180
- Mage::throwException($message);
181
- }
182
-
183
- if (empty($importData['sku'])) {
184
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
185
- Mage::throwException($message);
186
- }
187
- $product->setStoreId($store->getId());
188
- $productId = $product->getIdBySku($importData['sku']);
189
-
190
- if ($productId) {
191
- $product->load($productId);
192
-
193
- // check import type (Import only price & qty or all product info)
194
- if('import_price_stock' == Mage::getStoreConfig('importprod_root/importprod/import_only_prices_stock',
195
- $storeId)) {
196
- $product->setPrice($importData['price']);
197
- $product->setStockData(array('qty' => $importData['qty']));
198
- $product->save();
199
- return true;
200
- }
201
-
202
- }
203
- else {
204
- $productTypes = $this->getProductTypes();
205
- $productAttributeSets = $this->getProductAttributeSets();
206
-
207
- /**
208
- * Check product define type
209
- */
210
- if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) {
211
- $value = isset($importData['type']) ? $importData['type'] : '';
212
- $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'type');
213
- Mage::throwException($message);
214
- }
215
- $product->setTypeId($productTypes[strtolower($importData['type'])]);
216
- /**
217
- * Check product define attribute set
218
- */
219
- if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) {
220
- /*$value = isset($importData['attribute_set']) ? $importData['attribute_set'] : '';
221
- $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'attribute_set');
222
- Mage::throwException($message);*/
223
- }
224
- $product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]);
225
-
226
- foreach ($this->_requiredFields as $field) {
227
- $attribute = $this->getAttribute($field);
228
- if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) {
229
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" for new products not defined', $field);
230
- Mage::throwException($message);
231
- }
232
- }
233
- }
234
-
235
- $this->setProductTypeInstance($product);
236
 
237
- if (isset($importData['category_ids'])) {
238
- $product->setCategoryIds($importData['category_ids']);
239
- }
240
- /* if category name is in csv file */
241
- if (isset($importData['categories'])) {
242
-
243
-
244
- //get IceImport configs
245
- $storeId = $store->getId();
246
- $categoriesToActiveConf = Mage::getStoreConfig('importprod_root/importprod/category_active',
247
- $storeId);
248
- $cronScheduleConf = Mage::getStoreConfig('importprod_root/importprod/import_schedule',
249
- $storeId);
250
- $importImagesConf = Mage::getStoreConfig('importprod_root/importprod/import_images',
251
- $storeId);
252
-
253
-
254
-
255
-
256
- if (!empty($importData['leader_categories'])) {
257
- $leader_categories = $importData['leader_categories'];
258
- $leader_store = $this->getStoreByCode($importData['leader_store']);
259
- } else {
260
- $leader_categories = '';
261
- $leader_store = '';
262
- }
263
-
264
- $unspsc = '';
265
- if (!empty($importData['unspsc'])) {
266
- $unspsc = $importData['unspsc'];
267
- }
268
-
269
- $unspscPath = '';
270
- if (!empty($importData['unspsc_path'])) {
271
- $unspscPath = $importData['unspsc_path'];
272
- } else {
273
- $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC not defined in store');
274
- Mage::throwException($message);
275
- }
276
 
277
- $categoryIds = $this->_addCategories($importData['categories'], $store, $leader_store, $unspsc, $unspscPath, $categoriesToActiveConf);
278
- if ($categoryIds) {
279
-
280
- // check, that's product exist
281
- $oldProductId = $product->getIdBySku($importData['sku']);
282
-
283
- if ($oldProductId) {
284
- $oldCategoryIds = Mage::getModel('catalog/product')
285
- ->load($oldProductId)
286
- ->getCategoryIds();
287
- $categoryIds .= ','.implode(',', $oldCategoryIds);
288
- }
289
-
290
- $product->setCategoryIds($categoryIds);
291
- }
292
- }
293
- foreach ($this->_ignoreFields as $field) {
294
- if (isset($importData[$field])) {
295
- unset($importData[$field]);
296
- }
297
- }
298
-
299
- if ($store->getId() != 0) {
300
- $websiteIds = $product->getWebsiteIds();
301
- if (!is_array($websiteIds)) {
302
- $websiteIds = array();
303
- }
304
- if (!in_array($store->getWebsiteId(), $websiteIds)) {
305
- $websiteIds[] = $store->getWebsiteId();
306
- }
307
- $product->setWebsiteIds($websiteIds);
308
- }
309
-
310
- if (isset($importData['websites'])) {
311
- $websiteIds = $product->getWebsiteIds();
312
- if (!is_array($websiteIds)) {
313
- $websiteIds = array();
314
- }
315
- $websiteCodes = explode(',', $importData['websites']);
316
- foreach ($websiteCodes as $websiteCode) {
317
- try {
318
- $website = Mage::app()->getWebsite(trim($websiteCode));
319
- if (!in_array($website->getId(), $websiteIds)) {
320
- $websiteIds[] = $website->getId();
321
- }
322
- }
323
- catch (Exception $e) {}
324
- }
325
- $product->setWebsiteIds($websiteIds);
326
- unset($websiteIds);
327
- }
328
-
329
- foreach ($importData as $field => $value) {
330
- if (in_array($field, $this->_inventoryFields)) {
331
- continue;
332
- }
333
- if (in_array($field, $this->_imageFields)) {
334
- continue;
335
- }
336
- $attribute = $this->getAttribute($field);
337
- if (!$attribute) {
338
-
339
- if(strpos($field,':')!==FALSE && strlen($value)) {
340
- $values=explode('|',$value);
341
- if(count($values)>0) {
342
- @list($title,$type,$is_required,$sort_order) = explode(':',$field);
343
- $title = ucfirst(str_replace('_',' ',$title));
344
- $custom_options[] = array(
345
- 'is_delete'=>0,
346
- 'title'=>$title,
347
- 'previous_group'=>'',
348
- 'previous_type'=>'',
349
- 'type'=>$type,
350
- 'is_require'=>$is_required,
351
- 'sort_order'=>$sort_order,
352
- 'values'=>array()
353
- );
354
- foreach($values as $v) {
355
- $parts = explode(':',$v);
356
- $title = $parts[0];
357
- if(count($parts)>1) {
358
- $price_type = $parts[1];
359
- } else {
360
- $price_type = 'fixed';
361
- }
362
- if(count($parts)>2) {
363
- $price = $parts[2];
364
- } else {
365
- $price =0;
366
- }
367
- if(count($parts)>3) {
368
- $sku = $parts[3];
369
- } else {
370
- $sku='';
371
- }
372
- if(count($parts)>4) {
373
- $sort_order = $parts[4];
374
- } else {
375
- $sort_order = 0;
376
- }
377
- switch($type) {
378
- case 'file':
379
- /* TODO */
380
- break;
381
-
382
- case 'field':
383
- case 'area':
384
- $custom_options[count($custom_options) - 1]['max_characters'] = $sort_order;
385
- /* NO BREAK */
386
-
387
- case 'date':
388
- case 'date_time':
389
- case 'time':
390
- $custom_options[count($custom_options) - 1]['price_type'] = $price_type;
391
- $custom_options[count($custom_options) - 1]['price'] = $price;
392
- $custom_options[count($custom_options) - 1]['sku'] = $sku;
393
- break;
394
-
395
- case 'drop_down':
396
- case 'radio':
397
- case 'checkbox':
398
- case 'multiple':
399
- default:
400
- $custom_options[count($custom_options) - 1]['values'][]=array(
401
- 'is_delete'=>0,
402
- 'title'=>$title,
403
- 'option_type_id'=>-1,
404
- 'price_type'=>$price_type,
405
- 'price'=>$price,
406
- 'sku'=>$sku,
407
- 'sort_order'=>$sort_order,
408
- );
409
- break;
410
- }
411
- }
412
- }
413
- }
414
-
415
- continue;
416
- }
417
-
418
- $isArray = false;
419
- $setValue = $value;
420
-
421
- if ($attribute->getFrontendInput() == 'multiselect') {
422
- $value = explode(self::MULTI_DELIMITER, $value);
423
- $isArray = true;
424
- $setValue = array();
425
- }
426
-
427
- if ($value && $attribute->getBackendType() == 'decimal') {
428
- $setValue = $this->getNumber($value);
429
- }
430
-
431
-
432
- if ($attribute->usesSource()) {
433
- $options = $attribute->getSource()->getAllOptions(false);
434
-
435
- if ($isArray) {
436
- foreach ($options as $item) {
437
- if (in_array($item['label'], $value)) {
438
- $setValue[] = $item['value'];
439
- }
440
- }
441
- }
442
- else {
443
- $setValue = null;
444
- foreach ($options as $item) {
445
- if ($item['label'] == $value) {
446
- $setValue = $item['value'];
447
- }
448
- }
449
- }
450
- }
451
-
452
- $product->setData($field, $setValue);
453
- }
454
-
455
- if (!$product->getVisibility()) {
456
- $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
457
- }
458
-
459
- $stockData = array();
460
- $inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()])
461
- ? $this->_inventoryFieldsProductTypes[$product->getTypeId()]
462
- : array();
463
- foreach ($inventoryFields as $field) {
464
- if (isset($importData[$field])) {
465
- if (in_array($field, $this->_toNumber)) {
466
- $stockData[$field] = $this->getNumber($importData[$field]);
467
- }
468
- else {
469
- $stockData[$field] = $importData[$field];
470
- }
471
- }
472
- }
473
- $product->setStockData($stockData);
474
-
475
- $imageData = array();
476
- foreach ($this->_imageFields as $field) {
477
- if (!empty($importData[$field]) && $importData[$field] != 'no_selection') {
478
- if (!isset($imageData[$importData[$field]])) {
479
- $imageData[$importData[$field]] = array();
480
- }
481
- $imageData[$importData[$field]][] = $field;
482
- }
483
- }
484
-
485
- foreach ($imageData as $file => $fields) {
486
- try {
487
- $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
488
- }
489
- catch (Exception $e) {}
490
- }
491
-
492
- /**
493
- * Allows you to import multiple images for each product.
494
- * Simply add a 'gallery' column to the import file, and separate
495
- * each image with a semi-colon.
496
- */
497
- try {
498
- $galleryData = explode(';',$importData["gallery"]);
499
- foreach($galleryData as $gallery_img)
500
- /**
501
- * @param directory where import image resides
502
- * @param leave 'null' so that it isn't imported as thumbnail, base, or small
503
- * @param false = the image is copied, not moved from the import directory to it's new location
504
- * @param false = not excluded from the front end gallery
505
- */
506
- {
507
- $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false);
508
- }
509
- }
510
- catch (Exception $e) {}
511
- /* End Modification */
512
-
513
- $product->setIsMassupdate(true);
514
- $product->setExcludeUrlRewrite(true);
515
-
516
- $product->save();
517
- /* Add the custom options specified in the CSV import file */
518
-
519
- if(isset($custom_options)){
520
- if(count($custom_options)) {
521
- foreach($custom_options as $option) {
522
- try {
523
- $opt = Mage::getModel('catalog/product_option');
524
- $opt->setProduct($product);
525
- $opt->addOption($option);
526
- $opt->saveOptions();
527
- }
528
- catch (Exception $e) {}
529
- }
530
- }
531
- }
532
- return true;
533
  }
534
 
535
- protected function _coreSave(array $productData) {
536
- $preUrl = explode(' ', strtolower($productData['name']));
537
- $url = implode('_', $preUrl) . '_' . $productData['store'];
538
-
539
- if ($productData['productId'] === false) {
540
- // add product to store
541
- $coreSaveProduct = "INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`) VALUES
542
- (@product_entity_type_id, @attribute_set_id, 'simple', :sku)
543
- ON DUPLICATE KEY UPDATE
544
- `entity_type_id` = @product_entity_type_id,
545
- `attribute_set_id` = @attribute_set_id,
546
- `type_id` = 'simple',
547
- `sku` = :sku;
548
-
549
- SELECT @product_id := LAST_INSERT_ID();
550
- ";
551
-
552
- $this->_connRes->query($coreSaveProduct, array(':sku' => $productData['sku']));
553
- // get product ID
554
  $prodFetch = $this->_connRes->fetchRow('SELECT @product_id AS prod_id');
555
  $productId = $prodFetch['prod_id'];
556
- } else {
557
- $productId = (int)$productData['productId'];
558
- $coreSaveSQL .= "SELECT @product_id := " . $productId . "; ";
559
- }
560
- // eav varchar
561
- $coreSaveSQL .= "
562
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_varchar` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES
563
- (@product_entity_type_id, @name_id, 0, @product_id, :name),
564
- (@product_entity_type_id, @mpn_id, 0, @product_id, :mpn),
565
- (@product_entity_type_id, @brand_id, 0, @product_id, :brand_name),
566
- (@product_entity_type_id, @url_key_id, 0, @product_id, :url),
567
- (@product_entity_type_id, @delivery_id, 0, @product_id, :delivery_eta),
568
- (@product_entity_type_id, @name_id, :store_id, @product_id, :name),
569
- (@product_entity_type_id, @mpn_id, :store_id, @product_id, :mpn),
570
- (@product_entity_type_id, @brand_id, :store_id, @product_id, :brand_name),
571
- (@product_entity_type_id, @url_key_id, :store_id, @product_id, :url),
572
- (@product_entity_type_id, @delivery_id, :store_id, @product_id, :delivery_eta)
573
- ON DUPLICATE KEY UPDATE
574
- `entity_type_id` = VALUES (`entity_type_id`),
575
- `attribute_id` = VALUES (`attribute_id`),
576
- `store_id` = VALUES (`store_id`),
577
- `entity_id` = VALUES (`entity_id`),
578
- `value` = VALUES (`value`);
579
- ";
580
-
581
- // eav text
582
- $coreSaveSQL .= "
583
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_text` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES
584
- (@product_entity_type_id, @desc_id, 0, @product_id, :description),
585
- (@product_entity_type_id, @sh_desc_id, 0, @product_id, :short_description),
586
- (@product_entity_type_id, @desc_id, :store_id, @product_id, :description),
587
- (@product_entity_type_id, @sh_desc_id, :store_id, @product_id, :short_description)
588
- ON DUPLICATE KEY UPDATE
589
- `entity_type_id` = VALUES(`entity_type_id`),
590
- `attribute_id` = VALUES(`attribute_id`),
591
- `store_id` = VALUES(`store_id`),
592
- `entity_id` = VALUES(`entity_id`),
593
- `value` = VALUES(`value`);
594
- ";
595
-
596
- // eav decimal
597
- $coreSaveSQL .= "
598
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`) VALUES
599
- (@product_entity_type_id, @weight_id, 0, @product_id, :weight)
600
- ON DUPLICATE KEY UPDATE
601
- `entity_type_id` = @product_entity_type_id,
602
- `attribute_id` = @weight_id,
603
- `store_id` = 0,
604
- `entity_id` = @product_id,
605
- `value` = :weight;
606
- ";
607
-
608
- // eav int
609
- $coreSaveSQL .= "
610
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_int` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES
611
- (@product_entity_type_id, @status_id, 0, @product_id, :status),
612
- (@product_entity_type_id, @visibility_id, 0, @product_id, :visibility),
613
- (@product_entity_type_id, @status_id, :store_id, @product_id, :status),
614
- (@product_entity_type_id, @visibility_id, :store_id, @product_id, :visibility)
615
- ON DUPLICATE KEY UPDATE
616
- `entity_type_id` = VALUES(`entity_type_id`),
617
- `attribute_id` = VALUES(`attribute_id`),
618
- `store_id` = VALUES(`store_id`),
619
- `entity_id` = VALUES(`entity_id`),
620
- `value` = VALUES(`value`);
621
- ";
622
-
623
- // categories
624
- $categoryId = $productData['categoryId'];
625
- $coreSaveSQL .= "
626
- INSERT INTO `" . $this->_tablePrefix . "catalog_category_product` (`category_id`, `product_id`, `position`) VALUES
627
- (" . (int)$categoryId . ", @product_id, 1);";
628
-
629
- if ($productData['status'] == 'Enabled') {
630
- $productData['status'] = 1;
631
  } else {
632
- $productData['status'] = 0;
633
  }
634
 
635
- if ($productData['visibility'] == 'Catalog, Search') {
636
- $productData['visibility'] = 4;
637
- } else {
638
- $productData['visibility'] = 1;
639
- }
640
-
641
- try{
642
- $this->_connRes->query($coreSaveSQL, array(
643
- ':store_id' => $productData['storeId'],
644
- ':sku' => $productData['sku'],
645
- ':name' => $productData['name'],
646
- ':mpn' => $productData['mpn'],
647
- ':brand_name' => $productData['brand_name'],
648
- ':description' => $productData['description'],
649
- ':short_description' => $productData['short_description'],
650
- ':weight' => $productData['weight'],
651
- ':status' => $productData['status'],
652
- ':visibility' => $productData['visibility'],
653
- ':url' => $url,
654
- ':delivery_eta' => $productData['delivery_eta']
655
- ));
656
-
657
- } catch (Exception $e) {
658
- echo $e->getMessage();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
659
  }
660
 
661
- $this->_corePriceStock($productData['websiteId'], $productId, $productData['price'], $productData['qty']);
 
 
 
 
 
 
 
 
 
 
 
662
 
663
  }
664
 
665
- protected function _corePriceStock($website = false, $productId =false, $price =false, $qty =false) {
666
-
667
- $stockSaveSQL = "
668
- INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_item` (`product_id`, `stock_id`, `qty`, `is_in_stock`) VALUES
669
- (:product_id, (SELECT stock_id FROM `cataloginventory_stock` where stock_name = 'Default'), :qty,1)
670
- ON DUPLICATE KEY UPDATE
671
- `product_id` = :product_id,
672
- `stock_id` = (SELECT stock_id FROM `cataloginventory_stock` where stock_name = 'Default'),
673
- `qty` = :qty,
674
- `is_in_stock` = 1;
675
-
676
- INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_status` (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`) VALUES
677
- (:product_id, :webisteId, @stock_id, :qty, 1)
678
- ON DUPLICATE KEY UPDATE
679
- `product_id` = :product_id,
680
- `website_id` = :webisteId,
681
- `stock_id` = @stock_id,
682
- `qty` = :qty,
683
- `stock_status` = 1;
684
-
685
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_website` (`product_id`, `website_id`) VALUES
686
- (:product_id, :webisteId)
687
- ON DUPLICATE KEY UPDATE
688
- `product_id` = :product_id,
689
- `website_id` = :webisteId;
690
-
691
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`) VALUES
692
- (@product_entity_type_id, @price_id, 0, :product_id, :price)
693
- ON DUPLICATE KEY UPDATE
694
- `entity_type_id` = @product_entity_type_id,
695
- `attribute_id` = @price_id,
696
- `store_id` = 0,
697
- `entity_id` = :product_id,
698
- `value` = :price;
 
 
 
 
 
699
  ";
700
 
701
  $this->_connRes->query($stockSaveSQL, array(
702
- ':webisteId' => ($website) ? $website : 0,
703
- ':product_id' => $productId,
704
- ':price' => $price,
705
- ':qty' => $qty
706
- ));
 
 
707
  }
708
-
709
- protected function _addCategories($categories, $storeId, $leader_store, $unspsc, $unspscPath, $categoryActive = 1) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710
  // check if product exists
711
  $categoryId = $this->_getCategoryIdByUnspsc($unspsc);
712
  if (!empty($categoryId)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
713
  if ( 1 == $categoryActive) {
714
  $unspscArray = explode('/', $unspscPath);
715
  if ($unspscArray) {
@@ -729,13 +406,12 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
729
  }
730
  return $categoryId;
731
  } else {
732
-
733
  // merge unspcs to current name in unspcs & name path's
734
  $nameArray = explode('/', $categories);
735
  $unspscArray = explode('/', $unspscPath);
736
 
737
  if (count($nameArray) != count($unspscArray)) {
738
- $message = Mage::helper('catalog')->__('Skip import row, categories data is invaled');
739
  Mage::throwException($message);
740
  }
741
 
@@ -772,66 +448,317 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
772
  return Mage::app()->getStore(1)->getRootCategoryId();
773
  } else {
774
  $categoryId = $this->_connRes->fetchRow("SELECT entity_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
775
- `value` = :unspsc AND attribute_id = @unspcs_id", array(':unspsc' => $unspcs));
776
  return ($categoryId['entity_id']) ? $categoryId['entity_id'] : null;
777
  }
778
  }
779
-
780
  protected function _buildCategoryTree($parrentCategoryId, $storeId, $pathArray, $categoryActive = 0) {
781
- for ($i = count($pathArray) -1; $i >= 0; $i--) {
782
- $category = $pathArray[$i];
783
- $parrentCategoryId = $this->_createCategory($parrentCategoryId, $category['unspsc'], $storeId, $category['name'], $categoryActive);
784
- }
785
 
786
- return $parrentCategoryId;
787
  }
788
 
789
  protected function _createCategory($parrentId, $unspsc, $storeId, $name, $categoryActive = 0) {
790
-
791
  $addCategory = "
792
- SELECT @tPath := `path`, @tLevel := `level` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE `entity_id` = :parrent_id;
793
- SET @tLevel = @tLevel +1;
794
-
795
- SET @path := CONCAT(@tPath, '/',(SELECT MAX(entity_id) FROM `catalog_category_entity`) +1 );
796
-
797
- INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity` (`entity_type_id`, `attribute_set_id`,
798
- `parent_id`, `created_at`,
799
- `path`, `position`,
800
- `level`, `children_count`)
801
  VALUES
802
- (@category_entity_type_id, 0, :parrent_id, NOW(), @path, 1, @tLevel, 0);
803
-
804
- SELECT @catId := LAST_INSERT_ID();
805
-
806
- UPDATE `" . $this->_tablePrefix . "catalog_category_entity` SET children_count = children_count +1 WHERE entity_id = :parrent_id;
807
-
808
- INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`,
809
  `store_id`, `entity_id`, `value`)
810
- VALUES
811
- (@category_entity_type_id, @category_active_id, 0, @catId, :category_active),
812
- (@category_entity_type_id, @category_active_id, :store, @catId, :category_active),
813
- (@category_entity_type_id, @include_nav_bar_id, 0, @catId, 1),
814
- (@category_entity_type_id, @include_nav_bar_id, :store, @catId, 1);
815
-
816
-
817
- INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`,
818
  `store_id`, `entity_id`, `value`)
819
  VALUES
820
- (@category_entity_type_id, @category_name_id, 0, @catId, :category_name),
821
- (@category_entity_type_id, @category_name_id, :store, @catId, :category_name),
822
- (@category_entity_type_id, @unspcs_id, 0, @catId, :unspsc_val),
823
- (@category_entity_type_id, @unspcs_id, :store, @catId, :unspsc_val);
824
- ";
825
 
826
  $this->_connRes->query($addCategory, array(
827
- ':store' => $storeId,
828
- ':parrent_id' => $parrentId,
829
- ':category_name' => $name,
830
- ':unspsc_val' => $unspsc,
831
- ':category_active' => (int)$categoryActive
832
- ));
833
-
834
- $categoryIdFetch = $this->_connRes->fetchRow('SELECT @catId AS category_id');
835
- return $categoryIdFetch['category_id'];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836
  }
837
  }
 
12
  protected $_connRes = null;
13
  protected $_tablePrefix = '';
14
 
15
+ public function __construct(){
16
+
17
+ $this->_connRes = Mage::getSingleton('core/resource')->getConnection('core_write');
18
+ $tablePrefix = (array)Mage::getConfig()->getTablePrefix();
19
+ if (!empty($tablePrefix)) {
20
+ $this->_tablePrefix = $tablePrefix[0];
21
+ }
22
+
23
+ }
24
+
25
  public function saveRow(array $importData) {
26
 
27
+ // separate import data to eav & static
28
+ $sortedProductData = $this->_mapAttributes($importData);
29
+ $productData = $sortedProductData['productData'];
30
+ $iceimportAttributes = $sortedProductData['iceimportAttributes'];
31
+ $failedAttributes = $sortedProductData['failedAttributes'];
32
 
33
+ if (count($failedAttributes) > 0) {
34
+ echo 'Warning! Field(s) ' . implode(', ' , $failedAttributes) . ' not defined in store! Import continue. ';
35
+ }
 
36
 
37
+ // set website id
38
+ if (empty($iceimportAttributes['websites'])) {
39
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'website');
40
+ Mage::throwException($message);
 
41
  }
42
+ $website = Mage::app()->getWebsite(trim($iceimportAttributes['websites']));
43
+ $websiteId = $website->getId();
44
 
45
+ // set store id
46
+ if (empty($iceimportAttributes['store'])) {
47
  if (!is_null($this->getBatchParams('store'))) {
48
  $store = $this->getStoreById($this->getBatchParams('store'));
49
  } else {
50
  $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
51
  Mage::throwException($message);
52
  }
 
 
53
  }
54
+ $store = $this->getStoreByCode($iceimportAttributes['store']);
55
  if ($store === false) {
56
+ $message = Mage::helper('catalog')->__('Skip import row, store "%s" not exists', $iceimportAttributes['store']);
57
  Mage::throwException($message);
58
  }
59
+ $storeId = $store->getId();
60
 
61
+ // set type
62
+ if (empty($iceimportAttributes['type'])) {
63
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'type');
64
+ Mage::throwException($message);
65
+ }
66
+ $productType = $iceimportAttributes['type'];
67
 
68
+ // set attribute set
69
+ if (empty($iceimportAttributes['attribute_set'])) {
70
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'attribute_set');
71
+ Mage::throwException($message);
72
+ }
73
+ $attribute_set = $iceimportAttributes['attribute_set'];
 
 
74
 
75
+ // set sku
76
+ if (empty($iceimportAttributes['sku'])) {
77
  $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
78
  Mage::throwException($message);
79
  }
80
+ $sku = $iceimportAttributes['sku'];
81
 
82
+ // set category, unspsc, unspsc path
83
+ if (empty($iceimportAttributes['categories'])) {
84
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'categories');
85
+ Mage::throwException($message);
86
+ }
87
+ $category = $iceimportAttributes['categories'];
88
+ if (empty($iceimportAttributes['unspsc'])) {
89
+ $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC not defined in store');
90
+ Mage::throwException($message);
91
+ }
92
+ $unspsc = $iceimportAttributes['unspsc'];
93
+ if (empty($iceimportAttributes['unspsc_path'])) {
94
+ $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC path not defined in store');
95
+ Mage::throwException($message);
96
+ }
97
+ $unspscPath = $iceimportAttributes['unspsc_path'];
98
+
99
+ // set in / out of stock
100
+ $isInStock = 0;
101
+ if (!empty($iceimportAttributes['is_in_stock'])) {
102
+ $isInStock = $iceimportAttributes['is_in_stock'];
103
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
+ // set qty
106
+ $qty = 0;
107
+ if (!empty($iceimportAttributes['qty'])) {
108
+ $qty = $iceimportAttributes['qty'];
109
+ }
110
 
111
+ // set price
112
+ $price = 0.00;
113
+ if (!empty($iceimportAttributes['price'])) {
114
+ $price = $iceimportAttributes['price'];
115
+ }
 
 
116
 
117
+ // set status value
118
+ $statusValue = (!empty($iceimportAttributes['status']) && $iceimportAttributes['status'] == 'Enabled') ? 1 : 0;
119
+ $productData['int']['status'] = $statusValue;
120
+
121
+ // set visibility value
122
+ $visibilityValue = 1;
123
+ if (!empty($iceimportAttributes['visibility'])) {
124
+ switch ($iceimportAttributes['visibility']) {
125
+ case 'Not Visible Individually':
126
+ $visibilityValue = 1;
127
+ break;
128
+ case 'Catalog':
129
+ $visibilityValue = 2;
130
+ break;
131
+ case 'Search':
132
+ $visibilityValue = 3;
133
+ break;
134
+ case 'Catalog, Search':
135
+ $visibilityValue = 4;
136
+ break;
137
  }
138
+ }
139
+ $productData['int']['visibility'] = $visibilityValue;
140
+
141
+ // set product image
142
+ // TODO change 'icecat_url' to 'image' before production
143
+ $productImage = '';
144
+ if (!empty($iceimportAttributes['icecat_url'])) {
145
+ $productImage = $iceimportAttributes['icecat_url'];
146
+ }
147
+
148
+ // init general attributes query
149
+ $initAttributes = "
150
+ SELECT @product_entity_type_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
151
+ entity_type_code = 'catalog_product';
152
+ SELECT @category_entity_type_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
153
+ entity_type_code = 'catalog_category';
154
+ SELECT @attribute_set_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
155
+ entity_type_code = 'catalog_product';
156
+ SELECT @stock_id := `stock_id` FROM `" . $this->_tablePrefix . "cataloginventory_stock` WHERE
157
+ stock_name = 'Default';
158
+ SELECT @attribute_set_id := `attribute_set_id` FROM `" . $this->_tablePrefix . "eav_attribute_set`
159
+ WHERE attribute_set_name = :attribute_set AND entity_type_id =
160
+ (SELECT entity_type_id FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE entity_type_code = 'catalog_product');
161
+
162
+ SELECT @price_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
163
+ `attribute_code` = 'price' AND entity_type_id = @product_entity_type_id;
164
+
165
+ SELECT @unspcs_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
166
+ `attribute_code` = 'unspsc' AND entity_type_id = @category_entity_type_id;
167
+ SELECT @category_name_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
168
+ `attribute_code` = 'name' AND entity_type_id = @category_entity_type_id;
169
+ SELECT @category_active_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
170
+ `attribute_code` = 'is_active' AND entity_type_id = @category_entity_type_id;
171
+ SELECT @include_nav_bar_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
172
+ `attribute_code` = 'include_in_menu' AND entity_type_id = @category_entity_type_id;
173
+ ";
174
+ $this->_connRes->query($initAttributes, array(':attribute_set' => $iceimportAttributes['attribute_set']));
175
+
176
+ $prodIdFetch = $this->_connRes->fetchRow("SELECT entity_id FROM `" . $this->_tablePrefix . "catalog_product_entity` WHERE sku = :sku limit 1" , array(
177
+ ':sku' => $sku
178
+ ));
179
+ $productId = $prodIdFetch['entity_id'];
180
+
181
+ if (!empty($productId)) {
182
+ // check import type (Import only price & qty or all product info)
183
+ if('import_price_stock' == Mage::getStoreConfig('importprod_root/importprod/import_only_prices_stock',
184
+ $storeId)) {
185
+ $this->_corePriceStock($websiteId, $productId, $price, $qty, $sku, $isInStock);
186
+ return true;
187
  }
188
+ } else {
189
+ $productId = null;
190
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
+ // get category id
193
+ $categoriesToActiveConf = Mage::getStoreConfig('importprod_root/importprod/category_active',
194
+ $storeId);
195
+ $categoryId = $this->_addCategories($category, $storeId, $unspsc, $unspscPath, $categoriesToActiveConf);
196
+
197
+ // get url key
198
+ $url = '';
199
+ if (!empty($productData['varchar']['name'])) {
200
+ $preUrl = explode(' ', strtolower($productData['varchar']['name']));
201
+ $url = implode('-', $preUrl) . '-' . $iceimportAttributes['store'];
202
+ }
203
+ $productData['varchar']['url_key'] = $url;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
+ // if new product then ovewrride product id from null to id
206
+ $productId = $this->_coreSave($productData, $productId, $storeId, $sku, $categoryId);
207
+ // add product image to queue
208
+ if (Mage::getStoreConfig('importprod_root/importprod/import_images')) {
209
+ $this->_addImageToQueue($productId, $productImage);
210
+ }
211
+
212
+ // add price & stock
213
+ $this->_corePriceStock($websiteId, $productId, $price, $qty, $sku, $isInStock);
214
+
215
+ return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  }
218
 
219
+ protected function _coreSave(array $entityData, $productId = null, $storeId = 0, $sku, $categoryId) {
220
+
221
+ if ($productId === null) {
222
+ // add product to store
223
+ $coreSaveProduct = "INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`) VALUES
224
+ (@product_entity_type_id, @attribute_set_id, 'simple', :sku);
225
+ SELECT @product_id := LAST_INSERT_ID();
226
+ ";
227
+ $this->_connRes->query($coreSaveProduct, array(':sku' => $sku));
228
+ // get product ID
 
 
 
 
 
 
 
 
 
229
  $prodFetch = $this->_connRes->fetchRow('SELECT @product_id AS prod_id');
230
  $productId = $prodFetch['prod_id'];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  } else {
232
+ $coreSaveSQL .= "SELECT @product_id := " . (int)$productId . "; ";
233
  }
234
 
235
+ $bindArray[':store_id'] = $storeId;
236
+ foreach ($entityData as $type => $typeAttributes) {
237
+ if ($type != 'spec') {
238
+ $tailCoreSaveSQL = '';
239
+ $attributesInit = '';
240
+ if (!empty($typeAttributes)) {
241
+ $tailCoreSaveSQL .= "
242
+ INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_" . $type . "` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
243
+ foreach ($typeAttributes as $attribute => $value) {
244
+ $attributesInit .= "
245
+ SELECT @" . $attribute . "_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
246
+ `attribute_code` = '" . $attribute . "' AND entity_type_id = @product_entity_type_id;
247
+ ";
248
+ $tailCoreSaveSQL .= "
249
+ (@product_entity_type_id, @" . $attribute . "_id, 0, @product_id, :" . $attribute . " ),
250
+ (@product_entity_type_id, @" . $attribute . "_id, :store_id, @product_id, :" . $attribute . " ), ";
251
+ $bindArray[':' . $attribute] = $value;
252
+ }
253
+ $tailCoreSaveSQL = substr($tailCoreSaveSQL, 0, -2);
254
+ $tailCoreSaveSQL .= "
255
+ ON DUPLICATE KEY UPDATE
256
+ `value` = VALUES (`value`);
257
+ ";
258
+ }
259
+ $coreSaveSQL .= $attributesInit . $tailCoreSaveSQL;
260
+ } else {
261
+ foreach ($typeAttributes as $attribute => $attributeData) {
262
+ $prod_id_field = $attributeData['prod_id_field'];
263
+ $table = $attributeData['table'];
264
+ $field = $attributeData['field'];
265
+ $value = $attributeData['value'];
266
+ if (!empty($table) && !empty($field)) {
267
+ $coreSaveSQL .= "
268
+ UPDATE `" . $this->_tablePrefix . $table . "` SET `" . $field . "` = :" . $attribute . " WHERE `" . $prod_id_field . "` = @product_id;
269
+ ";
270
+ $bindArray[':' . $attribute] = $value;
271
+ }
272
+ }
273
+ }
274
  }
275
 
276
+ // categories
277
+ $coreSaveSQL .= "
278
+ INSERT INTO `" . $this->_tablePrefix . "catalog_category_product` (`category_id`, `product_id`, `position`) VALUES
279
+ (" . (int)$categoryId . ", @product_id, 1) ON DUPLICATE KEY UPDATE `position` = 1;
280
+ ";
281
+
282
+ try{
283
+ $this->_connRes->query($coreSaveSQL, $bindArray);
284
+ } catch(Exception $e) {
285
+ echo $e->getMessage();
286
+ }
287
+ return $productId;
288
 
289
  }
290
 
291
+ protected function _corePriceStock($website =0, $productId =false, $price =0.00, $qty =0.00, $sku =false, $isInStock =0) {
292
+
293
+ if (!$productId) {
294
+ $message = Mage::helper('catalog')->__('Skip import row, product_id for product "%s" not defined ', $sku);
295
+ Mage::throwException($message);
296
+ }
297
+
298
+ $stockSaveSQL = "
299
+ INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_item` (`product_id`, `stock_id`, `qty`, `is_in_stock`) VALUES
300
+ (:product_id, @stock_id, :qty,1)
301
+ ON DUPLICATE KEY UPDATE
302
+ `product_id` = :product_id,
303
+ `stock_id` = @stock_id,
304
+ `qty` = :qty,
305
+ `is_in_stock` = :is_in_stock;
306
+
307
+ INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_status` (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`) VALUES
308
+ (:product_id, :webisteId, @stock_id, :qty, 1)
309
+ ON DUPLICATE KEY UPDATE
310
+ `product_id` = :product_id,
311
+ `website_id` = :webisteId,
312
+ `stock_id` = @stock_id,
313
+ `qty` = :qty,
314
+ `stock_status` = :is_in_stock;
315
+
316
+ INSERT INTO `" . $this->_tablePrefix . "catalog_product_website` (`product_id`, `website_id`) VALUES
317
+ (:product_id, :webisteId)
318
+ ON DUPLICATE KEY UPDATE
319
+ `product_id` = :product_id,
320
+ `website_id` = :webisteId;
321
+
322
+ INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`) VALUES
323
+ (@product_entity_type_id, @price_id, 0, :product_id, :price)
324
+ ON DUPLICATE KEY UPDATE
325
+ `entity_type_id` = @product_entity_type_id,
326
+ `attribute_id` = @price_id,
327
+ `store_id` = 0,
328
+ `entity_id` = :product_id,
329
+ `value` = :price;
330
  ";
331
 
332
  $this->_connRes->query($stockSaveSQL, array(
333
+ ':webisteId' => $website,
334
+ ':product_id' => $productId,
335
+ ':price' => $price,
336
+ ':qty' => $qty,
337
+ ':is_in_stock' => $isInStock
338
+ ));
339
+
340
  }
341
+
342
+ protected function _addImageToQueue ($productId =false, $productImageUrl) {
343
+ $productImageUrl = trim($productImageUrl);
344
+ if ($productId && !empty($productImageUrl)) {
345
+ // add image if not exists to queue
346
+ $this->_connRes->query(" INSERT IGNORE INTO `" . $this->_tablePrefix . "capacity_product_image_queue` (`entity_id`, `image_url` ) VALUES
347
+ (:product_id, :image_url)
348
+ ", array(':product_id' => $productId,
349
+ ':image_url' => $productImageUrl));
350
+ }
351
+ }
352
+
353
+ public function getImageQueue(){
354
+ return $this->_connRes->fetchAll("SELECT `queue_id`, `entity_id`, `image_url` FROM `" . $this->_tablePrefix . "capacity_product_image_queue`
355
+ WHERE `is_downloaded` = 0
356
+ ");
357
+ }
358
+
359
+ public function setImageAsDownloaded($queueId =false){
360
+ if ($queueId) {
361
+ $this->_connRes->query("UPDATE `" . $this->_tablePrefix . "capacity_product_image_queue` SET is_downloaded = 1
362
+ WHERE queue_id = :queue_id", array(':queue_id' => $queueId));
363
+ }
364
+ }
365
+
366
+ protected function _addCategories($categories, $storeId, $unspsc, $unspscPath, $categoryActive = 1) {
367
+
368
  // check if product exists
369
  $categoryId = $this->_getCategoryIdByUnspsc($unspsc);
370
  if (!empty($categoryId)) {
371
+ // check category name to current store
372
+ $categoryBindArray = array(
373
+ ':store_id' => $storeId,
374
+ 'category_id' => $categoryId
375
+ );
376
+
377
+ $nameCheckerFetch = $this->_connRes->fetchRow("SELECT value_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
378
+ store_id = :store_id AND entity_id = :category_id AND attribute_id = @category_name_id
379
+ ", $categoryBindArray);
380
+ $nameChecker = $nameCheckerFetch['value_id'];
381
+ if (!$nameChecker) {
382
+ // add category name to current store
383
+ $categoryBindArray['category_name'] = array_pop(explode('/', $categories));
384
+ $this->_connRes->query("
385
+ INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES
386
+ (@category_entity_type_id, @category_name_id, :store_id, :category_id, :category_name)
387
+ ", $categoryBindArray);
388
+ }
389
+
390
  if ( 1 == $categoryActive) {
391
  $unspscArray = explode('/', $unspscPath);
392
  if ($unspscArray) {
406
  }
407
  return $categoryId;
408
  } else {
 
409
  // merge unspcs to current name in unspcs & name path's
410
  $nameArray = explode('/', $categories);
411
  $unspscArray = explode('/', $unspscPath);
412
 
413
  if (count($nameArray) != count($unspscArray)) {
414
+ $message = Mage::helper('catalog')->__('Skip import row, @categories data is invaled');
415
  Mage::throwException($message);
416
  }
417
 
448
  return Mage::app()->getStore(1)->getRootCategoryId();
449
  } else {
450
  $categoryId = $this->_connRes->fetchRow("SELECT entity_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
451
+ `value` = :unspsc AND attribute_id = @unspcs_id", array(':unspsc' => $unspcs));
452
  return ($categoryId['entity_id']) ? $categoryId['entity_id'] : null;
453
  }
454
  }
455
+
456
  protected function _buildCategoryTree($parrentCategoryId, $storeId, $pathArray, $categoryActive = 0) {
457
+ for ($i = count($pathArray) -1; $i >= 0; $i--) {
458
+ $category = $pathArray[$i];
459
+ $parrentCategoryId = $this->_createCategory($parrentCategoryId, $category['unspsc'], $storeId, $category['name'], $categoryActive);
460
+ }
461
 
462
+ return $parrentCategoryId;
463
  }
464
 
465
  protected function _createCategory($parrentId, $unspsc, $storeId, $name, $categoryActive = 0) {
466
+
467
  $addCategory = "
468
+ SELECT @tPath := `path`, @tLevel := `level` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE `entity_id` = :parrent_id;
469
+ SET @tLevel = @tLevel +1;
470
+
471
+ SET @path := CONCAT(@tPath, '/',(SELECT MAX(entity_id) FROM `" . $this->_tablePrefix . "catalog_category_entity`) +1 );
472
+
473
+ INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity` (`entity_type_id`, `attribute_set_id`,
474
+ `parent_id`, `created_at`,
475
+ `path`, `position`,
476
+ `level`, `children_count`)
477
  VALUES
478
+ (@category_entity_type_id, 0, :parrent_id, NOW(), @path, 1, @tLevel, 0);
479
+
480
+ SELECT @catId := LAST_INSERT_ID();
481
+
482
+ UPDATE `" . $this->_tablePrefix . "catalog_category_entity` SET children_count = children_count +1 WHERE entity_id = :parrent_id;
483
+
484
+ INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`,
485
  `store_id`, `entity_id`, `value`)
486
+ VALUES
487
+ (@category_entity_type_id, @category_active_id, 0, @catId, :category_active),
488
+ (@category_entity_type_id, @category_active_id, :store, @catId, :category_active),
489
+ (@category_entity_type_id, @include_nav_bar_id, 0, @catId, 1),
490
+ (@category_entity_type_id, @include_nav_bar_id, :store, @catId, 1);
491
+
492
+ INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`,
 
493
  `store_id`, `entity_id`, `value`)
494
  VALUES
495
+ (@category_entity_type_id, @category_name_id, 0, @catId, :category_name),
496
+ (@category_entity_type_id, @category_name_id, :store, @catId, :category_name),
497
+ (@category_entity_type_id, @unspcs_id, 0, @catId, :unspsc_val),
498
+ (@category_entity_type_id, @unspcs_id, :store, @catId, :unspsc_val);
499
+ ";
500
 
501
  $this->_connRes->query($addCategory, array(
502
+ ':store' => $storeId,
503
+ ':parrent_id' => $parrentId,
504
+ ':category_name' => $name,
505
+ ':unspsc_val' => $unspsc,
506
+ ':category_active' => (int)$categoryActive
507
+ ));
508
+
509
+ $categoryIdFetch = $this->_connRes->fetchRow('SELECT @catId AS category_id');
510
+ return $categoryIdFetch['category_id'];
511
+ }
512
+
513
+ protected function _mapAttributes(array $importData) {
514
+
515
+ // map iceimport attributes, skip some attributes
516
+ $iceAttributes = array();
517
+ foreach ($importData as $attribute => $value) {
518
+ // map iceimport attributes
519
+ // TODO change 'icecat_url' to 'image' before production
520
+ if ($attribute == 'type' ||
521
+ $attribute == 'sku' ||
522
+ $attribute == 'attribute_set' ||
523
+ $attribute == 'categories' ||
524
+ $attribute == 'unspsc' ||
525
+ $attribute == 'price' ||
526
+ $attribute == 'qty' ||
527
+ $attribute == 'status' ||
528
+ $attribute == 'visibility' ||
529
+ $attribute == 'store' ||
530
+ $attribute == 'websites' ||
531
+ $attribute == 'is_in_stock' ||
532
+ $attribute == 'icecat_url' ||
533
+ $attribute == 'unspsc_path') {
534
+
535
+ $iceAttributes[$attribute] = $value;
536
+ unset($importData[$attribute]);
537
+
538
+ }
539
+ // TODO change 'image' to 'icecat_url' to skip liste before production
540
+ // skip some attributes
541
+ if ($attribute == 'supplier_product_code' ||
542
+ $attribute == 'supplier' ||
543
+ $attribute == 'leader_categories' ||
544
+ $attribute == 'leader_store' ||
545
+ $attribute == 'sprice' ||
546
+ $attribute == 'euprice' ||
547
+ $attribute == 'icecat_product_id' ||
548
+ $attribute == 'icecat_category_id' ||
549
+ $attribute == 'icecat_vendor_id' ||
550
+ $attribute == 'icecat_quality' ||
551
+ $attribute == 'image' ||
552
+ $attribute == 'icecat_thumbnail_img' ||
553
+ $attribute == 'icecat_low_res_img' ||
554
+ $attribute == 'icecat_high_res_img' ||
555
+ $attribute == 'tax1' ||
556
+ $attribute == 'tax2' ||
557
+ $attribute == 'tax3' ||
558
+ $attribute == 'tax4' ||
559
+ $attribute == 'min_quantity' ||
560
+ $attribute == 'loms' ||
561
+ $attribute == 'tax_rate' ||
562
+ $attribute == 'image_label' ||
563
+ $attribute == 'links_title' ||
564
+ $attribute == 'small_image_label' ||
565
+ $attribute == 'tax_rate' ||
566
+ $attribute == 'gallery' ||
567
+ $attribute == 'weight_type' ||
568
+ $attribute == 'sku_type' ||
569
+ $attribute == 'manage_stock' ||
570
+ $attribute == 'minimal_price' ||
571
+ $attribute == 'required_options' ||
572
+ $attribute == 'samples_title' ||
573
+ $attribute == 'shipment_type' ||
574
+ $attribute == 'url_path' ||
575
+ $attribute == 'recurring_profile' ||
576
+ $attribute == 'product_keys') {
577
+
578
+ unset($importData[$attribute]);
579
+
580
+ }
581
+
582
+ }
583
+
584
+ // map default attributes
585
+ $productData = array();
586
+ foreach($this->_getDefaultAttributesList() as $backendType => $attributesList) {
587
+ if ($backendType != 'spec') {
588
+ foreach($attributesList as $attribute) {
589
+ if (isset($importData[$attribute])) {
590
+ $productData[$backendType][$attribute] = $importData[$attribute];
591
+ unset($importData[$attribute]);
592
+ }
593
+ }
594
+ } else {
595
+ foreach($attributesList as $attributeCode => $attributeSpecs) {
596
+ if (isset($importData[$attributeCode])) {
597
+ $attributeSpecs['value'] = $importData[$attributeCode];
598
+ $productData[$backendType][$attributeCode] = $attributeSpecs;
599
+ unset($importData[$attributeCode]);
600
+ }
601
+ }
602
+ }
603
+ }
604
+
605
+ // map custom attributes
606
+ if (!empty($importData)) {
607
+ foreach ($importData as $attributeCode => $value) {
608
+ $backendTypeFetch = $this->_connRes->fetchRow("SELECT backend_type FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = :code", array(':code' => $attributeCode));
609
+ $backendType = $backendTypeFetch['backend_type'];
610
+ if ($backendType != 'static' && !empty($backendType)) {
611
+ $productData[$backendType][$attributeCode] = $value;
612
+ unset($importData[$attributeCode]);
613
+ }
614
+ }
615
+ }
616
+
617
+ $failedAttributes = array();
618
+ if (count($importData) > 0) {
619
+ $failedAttributes = array_keys($importData);
620
+ }
621
+
622
+ return array(
623
+ 'iceimportAttributes' => $iceAttributes,
624
+ 'productData' => $productData,
625
+ 'failedAttributes' => $failedAttributes
626
+ );
627
+
628
+ }
629
+
630
+ protected function _getDefaultAttributesList () {
631
+ return array(
632
+ 'varchar' => array(
633
+ 'gift_message_available',
634
+ 'custom_design',
635
+ 'msrp_display_actual_price_type',
636
+ 'msrp_enabled',
637
+ 'options_container',
638
+ 'page_layout',
639
+ 'mpn',
640
+ 'brand_name',
641
+ 'name',
642
+ 'url_key',
643
+ 'meta_description',
644
+ 'meta_title'
645
+ ),
646
+ 'int' => array(
647
+ 'enable_googlecheckout',
648
+ 'is_recurring',
649
+ 'links_purchased_separately',
650
+ 'links_exist',
651
+ 'status',
652
+ 'visibility',
653
+ 'tax_class_id',
654
+ 'color',
655
+ 'price_view',
656
+ 'manufacturer'
657
+ ),
658
+ 'text' => array(
659
+ 'recurring_profile',
660
+ 'description',
661
+ 'custom_layout_update',
662
+ 'meta_keyword',
663
+ 'short_description'
664
+ ),
665
+ 'decimal' => array(
666
+ 'cost',
667
+ 'group_price',
668
+ 'weight',
669
+ 'special_price',
670
+ 'msrp'
671
+ ),
672
+ 'datetime' => array(
673
+ 'custom_design_from',
674
+ 'custom_design_to',
675
+ 'news_from_date',
676
+ 'news_to_date',
677
+ 'special_from_date',
678
+ 'special_to_date'
679
+ ),
680
+ 'spec' => array(
681
+ 'is_qty_decimal' => array(
682
+ 'prod_id_field' => 'product_id',
683
+ 'table' => 'cataloginventory_stock_item',
684
+ 'field' => 'is_qty_decimal'
685
+ ),
686
+ 'use_config_min_qty' => array(
687
+ 'prod_id_field' => 'product_id',
688
+ 'table' => 'cataloginventory_stock_item',
689
+ 'field' => 'use_config_min_qty'
690
+ ),
691
+ 'use_config_min_sale_qty' => array(
692
+ 'prod_id_field' => 'product_id',
693
+ 'table' => 'cataloginventory_stock_item',
694
+ 'field' => 'use_config_min_sale_qty'
695
+ ),
696
+ 'use_config_max_sale_qty' => array(
697
+ 'prod_id_field' => 'product_id',
698
+ 'table' => 'cataloginventory_stock_item',
699
+ 'field' => 'use_config_max_sale_qty'
700
+ ),
701
+ 'use_config_manage_stock' => array(
702
+ 'prod_id_field' => 'product_id',
703
+ 'table' => 'cataloginventory_stock_item',
704
+ 'field' => 'use_config_manage_stock'
705
+ ),
706
+ 'is_decimal_divided' => array(
707
+ 'prod_id_field' => 'product_id',
708
+ 'table' => 'cataloginventory_stock_item',
709
+ 'field' => 'is_decimal_divided'
710
+ ),
711
+ 'use_config_backorders' => array(
712
+ 'prod_id_field' => 'product_id',
713
+ 'table' => 'cataloginventory_stock_item',
714
+ 'field' => 'use_config_backorders'
715
+ ),
716
+ 'use_config_notify_stock_qty' => array(
717
+ 'prod_id_field' => 'product_id',
718
+ 'table' => 'cataloginventory_stock_item',
719
+ 'field' => 'use_config_notify_stock_qty'
720
+ ),
721
+ 'max_sale_qty' => array(
722
+ 'prod_id_field' => 'product_id',
723
+ 'table' => 'cataloginventory_stock_item',
724
+ 'field' => 'max_sale_qty'
725
+ ),
726
+ 'min_sale_qty' => array(
727
+ 'prod_id_field' => 'product_id',
728
+ 'table' => 'cataloginventory_stock_item',
729
+ 'field' => 'min_sale_qty'
730
+ ),
731
+ 'notify_stock_qty' => array(
732
+ 'prod_id_field' => 'product_id',
733
+ 'table' => 'cataloginventory_stock_item',
734
+ 'field' => 'notify_stock_qty'
735
+ ),
736
+ 'backorders' => array(
737
+ 'prod_id_field' => 'product_id',
738
+ 'table' => 'cataloginventory_stock_item',
739
+ 'field' => 'backorders'
740
+ ),
741
+ 'created_at' => array(
742
+ 'prod_id_field' => 'entity_id',
743
+ 'table' => 'catalog_product_entity',
744
+ 'field' => 'created_at'
745
+ ),
746
+ 'min_qty' => array(
747
+ 'prod_id_field' => 'product_id',
748
+ 'table' => 'cataloginventory_stock_item',
749
+ 'field' => 'min_qty'
750
+ ),
751
+ 'updated_at' => array(
752
+ 'prod_id_field' => 'entity_id',
753
+ 'table' => 'catalog_product_entity',
754
+ 'field' => 'updated_at'
755
+ )
756
+ )
757
+ );
758
+ }
759
+
760
+ public function testId() {
761
+ return $this->_connRes->fetchRow('SELECT * from prefcatalog_category_product order by product_id desc limit 1');
762
  }
763
  }
764
+
app/code/community/Capacitywebsolutions/Importproduct/Model/Observer.php CHANGED
@@ -14,7 +14,7 @@ class Capacitywebsolutions_Importproduct_Model_Observer
14
  */
15
  public function load() {
16
 
17
- /*
18
  // test load
19
  $con = mysql_connect('localhost', 'test', 'test');
20
  $db = mysql_select_db('catch');
@@ -75,7 +75,45 @@ class Capacitywebsolutions_Importproduct_Model_Observer
75
  }
76
 
77
  }
78
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  foreach ($profile->getExceptions() as $e) {
80
  Mage::log($e->getMessage(),null,$logFileName);
81
  }
@@ -92,6 +130,7 @@ class Capacitywebsolutions_Importproduct_Model_Observer
92
  Mage::log($e->getMessage(), null, $logFileName);
93
  }
94
 
 
95
  }
96
 
97
  }
14
  */
15
  public function load() {
16
 
17
+ /*
18
  // test load
19
  $con = mysql_connect('localhost', 'test', 'test');
20
  $db = mysql_select_db('catch');
75
  }
76
 
77
  }
78
+
79
+ // download & set product images
80
+ $queueList = $adapter->getImageQueue();
81
+ if (count($queueList) > 0) {
82
+ $mediaDir = Mage::getBaseDir('media');
83
+ foreach ($queueList as $queue) {
84
+ $queueId = $queue['queue_id'];
85
+ $productId = $queue['entity_id'];
86
+ $imageUrl = $queue['image_url'];
87
+ // TODO remove hardcode
88
+ $imageUrl = 'http://magento17.batavi.org/media/download.jpg';
89
+
90
+ $preImageName = explode('/', $imageUrl);
91
+ $imageName = array_pop($preImageName);
92
+ if (file_exists($mediaDir . DS . $imageName)) {
93
+ // TODO remove rand()
94
+ $imageName = rand() .'_'. time() . $imageName;
95
+ }
96
+
97
+ if(file_put_contents($mediaDir . DS . $imageName, file_get_contents($imageUrl))) {
98
+ $product = Mage::getModel('catalog/product')->load($productId);
99
+ $product->addImageToMediaGallery($mediaDir . DS . $imageName,
100
+ array('image', 'small_image', 'thumbnail'),
101
+ true, true
102
+ );
103
+ $product->save();
104
+ $adapter->setImageAsDownloaded($queueId);
105
+ echo $product->getCategory() . '<br>';
106
+ unset($product);
107
+ } else {
108
+ Mage::log('Unable download file to ' . $productId, $logFileName);
109
+ continue;
110
+ }
111
+ }
112
+ }
113
+
114
+ $processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
115
+ $processes->walk('reindexAll');
116
+
117
  foreach ($profile->getExceptions() as $e) {
118
  Mage::log($e->getMessage(),null,$logFileName);
119
  }
130
  Mage::log($e->getMessage(), null, $logFileName);
131
  }
132
 
133
+ // get prouct download queue
134
  }
135
 
136
  }
app/code/community/Capacitywebsolutions/Importproduct/Model/System/Config/Categoryactivity.php CHANGED
@@ -2,11 +2,10 @@
2
 
3
  class Capacitywebsolutions_Importproduct_Model_System_Config_Categoryactivity
4
  {
5
- public function toOptionArray()
6
- {
7
  $paramsArray = array(
8
- 1 => 'Yes',
9
- 0 => 'No'
10
  );
11
  return $paramsArray;
12
  }
2
 
3
  class Capacitywebsolutions_Importproduct_Model_System_Config_Categoryactivity
4
  {
5
+ public function toOptionArray(){
 
6
  $paramsArray = array(
7
+ 1 => 'Yes',
8
+ 0 => 'No'
9
  );
10
  return $paramsArray;
11
  }
app/code/community/Capacitywebsolutions/Importproduct/Model/System/Config/Imageimport.php CHANGED
@@ -5,8 +5,8 @@ class Capacitywebsolutions_Importproduct_Model_System_Config_Imageimport
5
  public function toOptionArray()
6
  {
7
  $paramsArray = array(
8
- 'import_images_true' => 'Yes',
9
- 'import_images_false' => 'No'
10
  );
11
  return $paramsArray;
12
  }
5
  public function toOptionArray()
6
  {
7
  $paramsArray = array(
8
+ '1' => 'Yes',
9
+ '0' => 'No'
10
  );
11
  return $paramsArray;
12
  }
app/code/community/Capacitywebsolutions/Importproduct/etc/config.xml CHANGED
@@ -6,16 +6,16 @@
6
  </Capacitywebsolutions_Importproduct>
7
  </modules>
8
  <global>
9
- <models>
10
- <catalog>
11
- <rewrite>
12
- <!-- Override Mage_Catalog_Model_Convert_Adapter_Product -->
13
- <convert_adapter_product>Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product</convert_adapter_product>
14
- </rewrite>
15
- </catalog>
16
- <importproduct>
17
- <class>Capacitywebsolutions_Importproduct_Model</class>
18
- </importproduct>
19
  </models>
20
  <resources>
21
  <importproduct_setup>
6
  </Capacitywebsolutions_Importproduct>
7
  </modules>
8
  <global>
9
+ <models>
10
+ <catalog>
11
+ <rewrite>
12
+ <!-- Override Mage_Catalog_Model_Convert_Adapter_Product -->
13
+ <convert_adapter_product>Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product</convert_adapter_product>
14
+ </rewrite>
15
+ </catalog>
16
+ <importproduct>
17
+ <class>Capacitywebsolutions_Importproduct_Model</class>
18
+ </importproduct>
19
  </models>
20
  <resources>
21
  <importproduct_setup>
app/code/community/Capacitywebsolutions/Importproduct/etc/system.xml CHANGED
@@ -37,9 +37,8 @@
37
  <show_in_website>1</show_in_website>
38
  <show_in_store>1</show_in_store>
39
  </import_only_prices_stock>
40
- <!--
41
- <import_images>
42
- <label>Import product images</label>
43
  <frontend_type>select</frontend_type>
44
  <source_model>importproduct/system_config_imageimport</source_model>
45
  <sort_order>30</sort_order>
@@ -47,18 +46,9 @@
47
  <show_in_website>1</show_in_website>
48
  <show_in_store>1</show_in_store>
49
  </import_images>
50
- <import_schedule>
51
- <label>Import schedule</label>
52
- <frontend_type>text</frontend_type>
53
- <sort_order>40</sort_order>
54
- <show_in_default>1</show_in_default>
55
- <show_in_website>1</show_in_website>
56
- <show_in_store>1</show_in_store>
57
- </import_schedule>
58
- -->
59
  </fields>
60
  </importprod>
61
  </groups>
62
  </importprod_root>
63
  </sections>
64
- </config>
37
  <show_in_website>1</show_in_website>
38
  <show_in_store>1</show_in_store>
39
  </import_only_prices_stock>
40
+ <import_images translate="label">
41
+ <label>Import product images (via Cron)</label>
 
42
  <frontend_type>select</frontend_type>
43
  <source_model>importproduct/system_config_imageimport</source_model>
44
  <sort_order>30</sort_order>
46
  <show_in_website>1</show_in_website>
47
  <show_in_store>1</show_in_store>
48
  </import_images>
 
 
 
 
 
 
 
 
 
49
  </fields>
50
  </importprod>
51
  </groups>
52
  </importprod_root>
53
  </sections>
54
+ </config>
app/code/community/Capacitywebsolutions/Importproduct/sql/importproduct_setup/mysql4-install-0.1.0.php CHANGED
@@ -33,22 +33,19 @@
33
  (`entity_type_id`, `attribute_id`, `entity_id`, `value`)
34
  SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
35
  FROM `{$installer->getTable('catalog_category_entity')}`;
36
- ");
37
 
38
- /*
39
- // set attribute to root category
40
- Mage::getModel('catalog/category')
41
- ->load(1)
42
- ->setImportedCatId(0)
43
- ->setInitialSetupFlag(true)
44
- ->save();
 
 
 
 
45
 
46
- Mage::getModel('catalog/category')
47
- ->load(2)
48
- ->setImportedCatId(0)
49
- ->setInitialSetupFlag(true)
50
- ->save();
51
- */
52
  $installer->endSetup();
53
 
54
  ?>
33
  (`entity_type_id`, `attribute_id`, `entity_id`, `value`)
34
  SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
35
  FROM `{$installer->getTable('catalog_category_entity')}`;
 
36
 
37
+ CREATE TABLE IF NOT EXISTS `{$installer->getTable('capacity_product_image_queue')}`
38
+ (
39
+ `queue_id` INT(10) NOT NULL AUTO_INCREMENT,
40
+ `entity_id` INT(10) UNSIGNED NOT NULL,
41
+ `image_url` VARCHAR(255) NOT NULL,
42
+ `is_downloaded` TINYINT NOT NULL DEFAULT 0,
43
+ PRIMARY KEY(`queue_id`),
44
+ UNIQUE KEY (`entity_id`, `image_url`),
45
+ CONSTRAINT `FK_CAP_PRD_IMG_QUEUE_ENTT_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `{$installer->getTable('catalog_product_entity')}` (`entity_id`) ON DELETE CASCADE
46
+ )ENGINE=InnoDB CHARSET=utf8 COMMENT='Table to manage product image import';
47
+ ");
48
 
 
 
 
 
 
 
49
  $installer->endSetup();
50
 
51
  ?>
package.xml CHANGED
@@ -1,21 +1,18 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>ice_import</name>
4
- <version>1.1.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v.3</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>Import categories &amp; products</summary>
10
  <description>This extension can use to import products with categories, multiple images and custom options from CSV file.</description>
11
- <notes>- Improvement of category tree import&#xD;
12
- - Improvement to speed of import&#xD;
13
- - Added possibility to run import by Cron&#xD;
14
- - Added new field delivery_eta</notes>
15
  <authors><author><name>Iceshop</name><user>Iceshop</user><email>support@iceshop.nl</email></author></authors>
16
- <date>2012-09-03</date>
17
- <time>10:33:38</time>
18
- <contents><target name="magecommunity"><dir name="Capacitywebsolutions"><dir name="Importproduct"><dir name="etc"><file name="config.xml" hash="287d723649aea3da75aa67f5b70fa632"/><file name="system.xml" hash="7a810bc95f4565c6bb09b8634e27109a"/></dir><dir name="Model"><dir name="Convert"><dir name="Adapter"><file name="Product.php" hash="02edef81c4ac6446d60e953e077fb54e"/></dir></dir><dir name="System"><dir name="Config"><file name="Categoryactivity.php" hash="b6765ac67a3dfda7c7fdafb09b0a9374"/><file name="Imageimport.php" hash="6d46e8ff52f3006a4461c0f0d479a190"/><file name="Lockproddetails.php" hash="32a823c0c914348585702536732fed3e"/></dir></dir><file name="Observer.php" hash="8ea437e36f8755a8578f31a8d0141f5a"/></dir><dir name="sql"><dir name="importproduct_setup"><file name="mysql4-install-0.1.0.php" hash="d20ba6989d47a7e3bb896f4e82172303"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Capacitywebsolutions_Importproduct.xml" hash="02cb79dd6ed129ce3aa5305e1bfb4bbf"/></dir></target></contents>
19
  <compatible/>
20
  <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
21
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>ice_import</name>
4
+ <version>1.2.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v.3</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>Import categories &amp; products</summary>
10
  <description>This extension can use to import products with categories, multiple images and custom options from CSV file.</description>
11
+ <notes>- Import any field from CSV to any text field</notes>
 
 
 
12
  <authors><author><name>Iceshop</name><user>Iceshop</user><email>support@iceshop.nl</email></author></authors>
13
+ <date>2012-10-02</date>
14
+ <time>21:31:49</time>
15
+ <contents><target name="magecommunity"><dir name="Capacitywebsolutions"><dir name="Importproduct"><dir name="etc"><file name="config.xml" hash="c59424e1f6806af1c50f322a84c262a9"/><file name="system.xml" hash="0f76e79143ac01dc948a1c4c5315c072"/></dir><dir name="Model"><dir name="Convert"><dir name="Adapter"><file name="Product.php" hash="85445d781feb85e4774aa9b0ccebae87"/></dir></dir><dir name="System"><dir name="Config"><file name="Categoryactivity.php" hash="2ddc7588dd1d038661f0017c8ff397a6"/><file name="Imageimport.php" hash="8b29e7bbf67d82a06f04ad9a63df26a0"/><file name="Lockproddetails.php" hash="32a823c0c914348585702536732fed3e"/></dir></dir><file name="Observer.php" hash="b9b2af7be5f23962ea234a58829d58ec"/></dir><dir name="sql"><dir name="importproduct_setup"><file name="mysql4-install-0.1.0.php" hash="8fe53e47d9d3a767dd843ab9a5cd3206"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Capacitywebsolutions_Importproduct.xml" hash="02cb79dd6ed129ce3aa5305e1bfb4bbf"/></dir></target></contents>
16
  <compatible/>
17
  <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
  </package>