ice_import - Version 1.3.0

Version Notes

- Add default tax attribute.
- Add support multisites stores category mapping.
- Removed skipped field warnings
- Fix some bugs...etc.
- Delete old not relevant products
- Set is anchor category property to yes
- Sort imported categories by name
- Added method to set categories, subcategories without products to inactive

Download this release

Release Info

Developer IceShop
Extension ice_import
Version 1.3.0
Comparing to
See all releases


Code changes from version 1.2.0 to 1.3.0

app/code/community/Capacitywebsolutions/Importproduct/Model/Convert/Adapter/Product.php CHANGED
@@ -23,18 +23,40 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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);
@@ -95,7 +117,7 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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'])) {
@@ -139,10 +161,9 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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
@@ -157,7 +178,7 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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;
@@ -170,7 +191,11 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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(
@@ -183,12 +208,17 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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);
@@ -204,6 +234,7 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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);
@@ -211,17 +242,60 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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));
@@ -280,12 +354,11 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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) {
@@ -368,65 +441,70 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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) {
393
$activeSetter = "INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
394
}
395
- foreach($unspscArray as $cat_unspsc) {
396
- $categoryParrentId = $this->_getCategoryIdByUnspsc($cat_unspsc);
397
- $activeSetter .= "(@category_entity_type_id, @category_active_id, :store_id, " . $categoryParrentId . ", 1),
398
- (@category_entity_type_id, @category_active_id, 0, " . $categoryParrentId . ", 1), ";
399
}
400
$activeSetter = substr($activeSetter, 0, -2);
401
$activeSetter .= "
402
ON DUPLICATE KEY UPDATE
403
`value` = 1
404
";
405
- $this->_connRes->query($activeSetter, array(':store_id' => $storeId));
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
-
418
- $categoryMergedArray = array(
419
- array(
420
- 'unspsc' => 'default_root',
421
- 'name' => 'Default category'
422
- )
423
- );
424
-
425
- for($i = 0; $i < count($unspscArray); $i++) {
426
- $categoryMergedArray[] = array('name' =>$nameArray[$i],
427
- 'unspsc' =>$unspscArray[$i]);
428
- }
429
430
// get max created parrent category
431
$categoryCreateArray = array();
432
for ($i = count($categoryMergedArray) -1; $i >= 0; $i--) {
@@ -443,6 +521,30 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
443
}
444
}
445
446
protected function _getCategoryIdByUnspsc($unspcs) {
447
if ($unspcs == 'default_root') {
448
return Mage::app()->getStore(1)->getRootCategoryId();
@@ -486,6 +588,8 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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
@@ -507,6 +611,7 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
507
));
508
509
$categoryIdFetch = $this->_connRes->fetchRow('SELECT @catId AS category_id');
510
return $categoryIdFetch['category_id'];
511
}
512
@@ -516,7 +621,6 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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' ||
@@ -529,14 +633,13 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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' ||
@@ -548,7 +651,7 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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' ||
@@ -558,7 +661,6 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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' ||
@@ -756,9 +858,30 @@ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends M
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
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
+ //Init session values to count total products and determine the last call of saveRow method
34
+ $session = Mage::getSingleton("core/session");
35
+ $import_total = $session->getData("import_total");
36
+ $counter = $session->getData("counter");
37
38
+ if (!isset($counter)) {
39
+ $session->setData("counter",1);
40
+ $counter = $session->getData("counter");
41
}
42
+
43
+ if (!isset($import_total)) {
44
+ $batchId = Mage::getSingleton('core/app')->getRequest()->getPost('batch_id', 0);
45
+ $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
46
+ $batchImportModel = $batchModel->getBatchImportModel();
47
+ $importIds = $batchImportModel->getIdCollection();
48
+ $import_total = count($importIds);
49
+ $session->setData("import_total",(int)$import_total);
50
+ } else if(isset($counter) && isset($import_total)) {
51
+ if($counter < $import_total) {
52
+ $session->setData("counter",(int)++$counter);
53
+ }
54
+ }
55
+
56
+ // mark product ice_import generic
57
+ $productData['varchar']['is_iceimport'] =1;
58
+
59
+ // set website id
60
if (empty($iceimportAttributes['websites'])) {
61
$message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'website');
62
Mage::throwException($message);
117
Mage::throwException($message);
118
}
119
$unspscPath = $iceimportAttributes['unspsc_path'];
120
+
121
// set in / out of stock
122
$isInStock = 0;
123
if (!empty($iceimportAttributes['is_in_stock'])) {
161
$productData['int']['visibility'] = $visibilityValue;
162
163
// set product image
164
$productImage = '';
165
+ if (!empty($iceimportAttributes['image'])) {
166
+ $productImage = $iceimportAttributes['image'];
167
}
168
169
// init general attributes query
178
stock_name = 'Default';
179
SELECT @attribute_set_id := `attribute_set_id` FROM `" . $this->_tablePrefix . "eav_attribute_set`
180
WHERE attribute_set_name = :attribute_set AND entity_type_id =
181
+ @product_entity_type_id;
182
183
SELECT @price_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
184
`attribute_code` = 'price' AND entity_type_id = @product_entity_type_id;
191
`attribute_code` = 'is_active' AND entity_type_id = @category_entity_type_id;
192
SELECT @include_nav_bar_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
193
`attribute_code` = 'include_in_menu' AND entity_type_id = @category_entity_type_id;
194
+ SELECT @category_is_anchor_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
195
+ `attribute_code` = 'is_anchor' AND entity_type_id = @category_entity_type_id;
196
";
197
+
198
+
199
$this->_connRes->query($initAttributes, array(':attribute_set' => $iceimportAttributes['attribute_set']));
200
201
$prodIdFetch = $this->_connRes->fetchRow("SELECT entity_id FROM `" . $this->_tablePrefix . "catalog_product_entity` WHERE sku = :sku limit 1" , array(
208
if('import_price_stock' == Mage::getStoreConfig('importprod_root/importprod/import_only_prices_stock',
209
$storeId)) {
210
$this->_corePriceStock($websiteId, $productId, $price, $qty, $sku, $isInStock);
211
+ $query = $this->_connRes->query('INSERT INTO iceimport_imported_product_ids (product_id, product_sku) VALUES (:prod_id, :sku)',array(':prod_id' => $productId,':sku' => $sku));
212
return true;
213
}
214
} else {
215
$productId = null;
216
}
217
+ $defaulttaxConf = (int)Mage::getStoreConfig('importprod_root/importprod/default_tax',$storeId);
218
+
219
+ if( !empty($defaulttaxConf) ) {
220
+ $productData['int']['tax_class_id'] = $defaulttaxConf;
221
+ }
222
// get category id
223
$categoriesToActiveConf = Mage::getStoreConfig('importprod_root/importprod/category_active',
224
$storeId);
234
235
// if new product then ovewrride product id from null to id
236
$productId = $this->_coreSave($productData, $productId, $storeId, $sku, $categoryId);
237
+
238
// add product image to queue
239
if (Mage::getStoreConfig('importprod_root/importprod/import_images')) {
240
$this->_addImageToQueue($productId, $productImage);
242
243
// add price & stock
244
$this->_corePriceStock($websiteId, $productId, $price, $qty, $sku, $isInStock);
245
+
246
+ $query = $this->_connRes->query('INSERT INTO iceimport_imported_product_ids (product_id, product_sku) VALUES (:prod_id, :sku)',array(':prod_id' => $productId,':sku' => $sku));
247
+
248
+ // Check if this is last imported product
249
+ // Do category sort and set categories without products to inactive
250
+ if (isset($counter) && isset($import_total) && ($counter==$import_total)) {
251
+ $catCollection = Mage::getModel('catalog/category')
252
+ ->getCollection()
253
+ ->addAttributeToSort('name', 'ASC');
254
+ $position = 1;
255
+
256
+ foreach ($catCollection as $category) {
257
+
258
+ $query = $this->_connRes->query("UPDATE `". $this->_tablePrefix . "catalog_category_entity` SET position = :position WHERE entity_id = :cat_id ", array(
259
+ ':position'=>$position,
260
+ ':cat_id'=> $category->getId()
261
+ ));
262
+
263
+ $query = "SELECT COUNT(*) FROM `" . $this->_tablePrefix . "catalog_category_product` WHERE category_id = :cat_id ";
264
+ $cat_products = $this->_connRes->fetchRow($query,array(
265
+ ':cat_id'=> $category->getId()
266
+ ));
267
+
268
+ if ($cat_products['COUNT(*)'] == 0) {
269
+ $query = "SELECT `entity_id` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE parent_id = :cat_id";
270
+ $child_cat = $this->_connRes->fetchAll($query,array(
271
+ ':cat_id'=> $category->getId()
272
+ ));
273
+
274
+ if (isset($child_cat) && count($child_cat) > 0) {
275
+ //Count child categories products and set them to inactive if they have no
276
+ $this->CountChildProd($child_cat);
277
+ } else {
278
+ $this->_connRes->query("UPDATE `" . $this->_tablePrefix . "catalog_category_entity_int`
279
+ SET `value` = 0 WHERE `attribute_id` = @category_active_id AND entity_id = :cat_id",array(
280
+ ':cat_id'=> $category->getId()
281
+ ));
282
+ }
283
+ }
284
+ $position++;
285
+ }
286
+
287
+ $session->unsetData('import_total');
288
+ $session->unsetData('counter');
289
+ }
290
+
291
return true;
292
}
293
294
protected function _coreSave(array $entityData, $productId = null, $storeId = 0, $sku, $categoryId) {
295
if ($productId === null) {
296
// add product to store
297
+ $coreSaveProduct = "INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`, `created_at`) VALUES
298
+ (@product_entity_type_id, @attribute_set_id, 'simple', :sku, NOW());
299
SELECT @product_id := LAST_INSERT_ID();
300
";
301
$this->_connRes->query($coreSaveProduct, array(':sku' => $sku));
354
";
355
356
try{
357
+ $query = $this->_connRes->query($coreSaveSQL, $bindArray);
358
} catch(Exception $e) {
359
echo $e->getMessage();
360
}
361
return $productId;
362
}
363
364
protected function _corePriceStock($website =0, $productId =false, $price =0.00, $qty =0.00, $sku =false, $isInStock =0) {
441
// check if product exists
442
$categoryId = $this->_getCategoryIdByUnspsc($unspsc);
443
if (!empty($categoryId)) {
444
+ // merge categories by unspsc
445
+ $categoryMergedArray = $this->_categoryMapper($categories, $unspscPath);
446
+ foreach ($categoryMergedArray as $category) {
447
+ $categoryName = $category['name'];
448
+ $categoryUnspsc = $category['unspsc'];
449
+ $categoryTreeId = $this->_getCategoryIdByUnspsc($categoryUnspsc);
450
+ // check category name to current store
451
+ $categoryBindArray = array(
452
+ ':store_id' => $storeId,
453
+ ':category_id' => $categoryTreeId
454
+ );
455
+ $nameCheckerFetch = $this->_connRes->fetchRow("SELECT value_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
456
+ store_id = :store_id AND entity_id = :category_id AND attribute_id = @category_name_id
457
+ ", $categoryBindArray);
458
+ $nameChecker = $nameCheckerFetch['value_id'];
459
+ if (!$nameChecker) {
460
+ // add category name to current store
461
+ $categoryBindArray[':category_name'] = $categoryName;
462
+ if (!empty($categoryBindArray[':category_id'])){
463
+ $this->_connRes->query("
464
+ INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES
465
+ (@category_entity_type_id, @category_name_id, :store_id, :category_id, :category_name)
466
+ ", $categoryBindArray);
467
+ }
468
+ }
469
}
470
+ if (1 == $categoryActive) {
471
+ // get current path of category
472
+ $categoryPath = $this->_connRes->fetchRow("SELECT path FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE entity_id = :entity_id",
473
+ array(':entity_id' => $categoryId));
474
+ $categoryPathArray = explode('/', $categoryPath['path']);
475
+ if ($categoryPathArray) {
476
$activeSetter = "INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
477
}
478
+
479
+ $falseCounter = 0;
480
+ foreach($categoryPathArray as $categoryId) {
481
+ $category = Mage::getModel('catalog/category')->load($categoryId);
482
+ $cid = $category->getId();
483
+ if (!empty($cid)) {
484
+ if (!empty($categoryId)) {
485
+ $activeSetter .= "(@category_entity_type_id, @category_active_id, :store_id, " . $categoryId . ", 1),
486
+ (@category_entity_type_id, @category_active_id, 0, " . $categoryId . ", 1), ";
487
+ } else {
488
+ $falseCounter++;
489
+ }
490
+ } else {
491
+ $falseCounter++;
492
+ }
493
}
494
$activeSetter = substr($activeSetter, 0, -2);
495
$activeSetter .= "
496
ON DUPLICATE KEY UPDATE
497
`value` = 1
498
";
499
+ if ($falseCounter < count($categoryPathArray)) {
500
+ $this->_connRes->query($activeSetter, array(':store_id' => $storeId));
501
+ }
502
}
503
return $categoryId;
504
} else {
505
506
+ // merge unspcs to current name in unspcs & name path's
507
+ $categoryMergedArray = $this->_categoryMapper($categories, $unspscPath);
508
// get max created parrent category
509
$categoryCreateArray = array();
510
for ($i = count($categoryMergedArray) -1; $i >= 0; $i--) {
521
}
522
}
523
524
+ protected function _categoryMapper($categoryPath, $unspscPath) {
525
+ $nameArray = explode('/', $categoryPath);
526
+ $unspscArray = explode('/', $unspscPath);
527
+
528
+ if (count($nameArray) != count($unspscArray)) {
529
+ $message = Mage::helper('catalog')->__('Skip import row, @categories data is invaled');
530
+ Mage::throwException($message);
531
+ }
532
+
533
+ $categoryMergedArray = array(
534
+ array(
535
+ 'unspsc' => 'default_root',
536
+ 'name' => 'Default category'
537
+ )
538
+ );
539
+
540
+ for($i = 0; $i < count($unspscArray); $i++) {
541
+ $categoryMergedArray[] = array('name' =>$nameArray[$i],
542
+ 'unspsc' =>$unspscArray[$i]);
543
+ }
544
+
545
+ return $categoryMergedArray;
546
+ }
547
+
548
protected function _getCategoryIdByUnspsc($unspcs) {
549
if ($unspcs == 'default_root') {
550
return Mage::app()->getStore(1)->getRootCategoryId();
588
VALUES
589
(@category_entity_type_id, @category_active_id, 0, @catId, :category_active),
590
(@category_entity_type_id, @category_active_id, :store, @catId, :category_active),
591
+ (@category_entity_type_id, @category_is_anchor_id, 0, @catId, 1),
592
+ (@category_entity_type_id, @category_is_anchor_id, :store, @catId, 1),
593
(@category_entity_type_id, @include_nav_bar_id, 0, @catId, 1),
594
(@category_entity_type_id, @include_nav_bar_id, :store, @catId, 1);
595
611
));
612
613
$categoryIdFetch = $this->_connRes->fetchRow('SELECT @catId AS category_id');
614
+
615
return $categoryIdFetch['category_id'];
616
}
617
621
$iceAttributes = array();
622
foreach ($importData as $attribute => $value) {
623
// map iceimport attributes
624
if ($attribute == 'type' ||
625
$attribute == 'sku' ||
626
$attribute == 'attribute_set' ||
633
$attribute == 'store' ||
634
$attribute == 'websites' ||
635
$attribute == 'is_in_stock' ||
636
+ $attribute == 'image' ||
637
$attribute == 'unspsc_path') {
638
639
$iceAttributes[$attribute] = $value;
640
unset($importData[$attribute]);
641
642
}
643
// skip some attributes
644
if ($attribute == 'supplier_product_code' ||
645
$attribute == 'supplier' ||
651
$attribute == 'icecat_category_id' ||
652
$attribute == 'icecat_vendor_id' ||
653
$attribute == 'icecat_quality' ||
654
+ $attribute == 'icecat_url' ||
655
$attribute == 'icecat_thumbnail_img' ||
656
$attribute == 'icecat_low_res_img' ||
657
$attribute == 'icecat_high_res_img' ||
661
$attribute == 'tax4' ||
662
$attribute == 'min_quantity' ||
663
$attribute == 'loms' ||
664
$attribute == 'image_label' ||
665
$attribute == 'links_title' ||
666
$attribute == 'small_image_label' ||
858
)
859
);
860
}
861
+
862
+ // Count child categories products and set them inactive if they have no
863
+ public function CountChildProd($child_cat) {
864
+ foreach ($child_cat as $cat) {
865
+ $query = "SELECT `entity_id` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE parent_id = :cat_id";
866
+ $child_cat = $this->_connRes->fetchAll($query,array(
867
+ ':cat_id'=> $cat['entity_id']
868
+ ));
869
870
+ $query = "SELECT COUNT(*) FROM `" . $this->_tablePrefix . "catalog_category_product` WHERE category_id = :cat_id ";
871
+ $cat_products = $this->_connRes->fetchRow($query,array(
872
+ ':cat_id'=> $cat['entity_id']
873
+ ));
874
+
875
+ if ($cat_products['COUNT(*)'] == 0 && empty($child_cat)) {
876
+ $this->_connRes->query("UPDATE `" . $this->_tablePrefix . "catalog_category_entity_int`
877
+ SET `value` = 0 WHERE `attribute_id` = @category_active_id AND entity_id = :cat_id",array(
878
+ ':cat_id'=> $cat['entity_id']
879
+ ));
880
+ } else if (!empty($child_cat)) {
881
+ $this->CountChildProd($child_cat);
882
+ }
883
+ }
884
}
885
+
886
}
887
app/code/community/Capacitywebsolutions/Importproduct/Model/Observer.php CHANGED
@@ -1,5 +1,4 @@
1
<?php
2
- /*m@rk*/
3
class Capacitywebsolutions_Importproduct_Model_Observer
4
{
5
@@ -13,15 +12,7 @@ class Capacitywebsolutions_Importproduct_Model_Observer
13
* @access publc
14
*/
15
public function load() {
16
-
17
- /*
18
- // test load
19
- $con = mysql_connect('localhost', 'test', 'test');
20
- $db = mysql_select_db('catch');
21
- $query = mysql_query("INSERT INTO detect (event, time) VALUES ('load called', NOW())");
22
- */
23
-
24
-
25
$profileId = 3;
26
$logFileName= 'test.log';
27
$recordCount = 0;
@@ -44,15 +35,19 @@ class Capacitywebsolutions_Importproduct_Model_Observer
44
$profile->run();
45
46
$batchModel = Mage::getSingleton('dataflow/batch');
47
if ($batchModel->getId()) {
48
if ($batchModel->getAdapter()) {
49
- $batchId = $batchModel->getId();
50
$batchImportModel = $batchModel->getBatchImportModel();
51
$importIds = $batchImportModel->getIdCollection();
52
53
$batchModel = Mage::getModel('dataflow/batch')->load($batchId);
54
$adapter = Mage::getModel($batchModel->getAdapter());
55
foreach ($importIds as $importId) {
56
$recordCount++;
57
try{
58
$batchImportModel->load($importId);
@@ -76,6 +71,27 @@ class Capacitywebsolutions_Importproduct_Model_Observer
76
77
}
78
79
// download & set product images
80
$queueList = $adapter->getImageQueue();
81
if (count($queueList) > 0) {
@@ -84,13 +100,10 @@ class Capacitywebsolutions_Importproduct_Model_Observer
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
@@ -110,6 +123,58 @@ class Capacitywebsolutions_Importproduct_Model_Observer
110
}
111
}
112
}
113
114
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
115
$processes->walk('reindexAll');
@@ -120,7 +185,8 @@ class Capacitywebsolutions_Importproduct_Model_Observer
120
121
}
122
}
123
-
124
echo 'Import Completed';
125
Mage::log("Import Completed",null,$logFileName);
126
1
<?php
2
class Capacitywebsolutions_Importproduct_Model_Observer
3
{
4
12
* @access publc
13
*/
14
public function load() {
15
+
16
$profileId = 3;
17
$logFileName= 'test.log';
18
$recordCount = 0;
35
$profile->run();
36
37
$batchModel = Mage::getSingleton('dataflow/batch');
38
+
39
if ($batchModel->getId()) {
40
if ($batchModel->getAdapter()) {
41
+ $batchId = $batchModel->getId();
42
$batchImportModel = $batchModel->getBatchImportModel();
43
$importIds = $batchImportModel->getIdCollection();
44
45
$batchModel = Mage::getModel('dataflow/batch')->load($batchId);
46
+
47
$adapter = Mage::getModel($batchModel->getAdapter());
48
+
49
foreach ($importIds as $importId) {
50
+
51
$recordCount++;
52
try{
53
$batchImportModel->load($importId);
71
72
}
73
74
+ // delete old products
75
+ $db_res = Mage::getSingleton('core/resource')->getConnection('core_write');
76
+ $tablePrefix = '';
77
+ $tPrefix = (array)Mage::getConfig()->getTablePrefix();
78
+ if (!empty($tPrefix)) {
79
+ $tablePrefix = $tPrefix[0];
80
+ }
81
+
82
+ try{
83
+ $db_res->query("SELECT @is_iceimport_id := `attribute_id` FROM " . $tablePrefix . "eav_attribute WHERE attribute_code = 'is_iceimport'");
84
+ $db_res->query("DELETE cpe FROM " . $tablePrefix . "catalog_product_entity AS cpe
85
+ JOIN " . $tablePrefix . "catalog_product_entity_varchar AS cpev ON cpe.entity_id = cpev.entity_id AND cpev.value = 1 AND cpev.attribute_id = @is_iceimport_id
86
+ LEFT JOIN iceimport_imported_product_ids AS iip ON cpe.entity_id = iip.product_id
87
+ WHERE iip.product_id IS NULL");
88
+ $db_res->query('DELETE FROM iceimport_imported_product_ids');
89
+
90
+ } catch (Exception $e) {
91
+ throw new Exception($e->getMessage());
92
+ }
93
+
94
+
95
// download & set product images
96
$queueList = $adapter->getImageQueue();
97
if (count($queueList) > 0) {
100
$queueId = $queue['queue_id'];
101
$productId = $queue['entity_id'];
102
$imageUrl = $queue['image_url'];
103
104
$preImageName = explode('/', $imageUrl);
105
$imageName = array_pop($preImageName);
106
if (file_exists($mediaDir . DS . $imageName)) {
107
$imageName = rand() .'_'. time() . $imageName;
108
}
109
123
}
124
}
125
}
126
+ // sort category in abc
127
+
128
+ $catCollection = Mage::getModel('catalog/category')
129
+ ->getCollection()
130
+ ->addAttributeToSort('name', 'ASC');
131
+ $position =1;
132
+ foreach($catCollection as $category) {
133
+ $catSource = Mage::getModel('catalog/category')->load($category->getId());
134
+ $catSource->setData('position', $position);
135
+ $catSource->save();
136
+ $query = "SELECT COUNT(*) FROM `" . $this->_tablePrefix . "catalog_category_product` WHERE category_id = :cat_id ";
137
+ $cat_products = $db_res->fetchRow($query,array(
138
+ ':cat_id'=> $category->getId()
139
+ ));
140
+
141
+ if ($cat_products['COUNT(*)'] == 0) {
142
+ $query = "SELECT `entity_id` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE parent_id = :cat_id";
143
+ $child_cat = $db_res->fetchAll($query,array(
144
+ ':cat_id'=> $category->getId()
145
+ ));
146
+ $cat_prod = 0;
147
+
148
+ if (isset($child_cat) && count($child_cat) > 0) {
149
+
150
+ foreach ($child_cat as $cat) {
151
+ $query = "SELECT COUNT(*) FROM `" . $this->_tablePrefix . "catalog_category_product` WHERE category_id = :cat_id ";
152
+ $cat_products = $db_res->fetchRow($query,array(
153
+ ':cat_id'=> $cat['entity_id']
154
+ ));
155
+
156
+ if ($cat_products['COUNT(*)'] != 0) {
157
+ $cat_prod = 1;
158
+ }
159
+
160
+ }
161
+
162
+ if ($cat_prod == 0) {
163
+ $db_res->query("UPDATE `" . $this->_tablePrefix . "catalog_category_entity_int`
164
+ SET `value` = 0 WHERE `attribute_id` = @category_active_id AND entity_id = :cat_id",array(
165
+ ':cat_id'=> $category->getId()
166
+ ));
167
+ }
168
+ } else {
169
+ $db_res->query("UPDATE `" . $this->_tablePrefix . "catalog_category_entity_int`
170
+ SET `value` = 0 WHERE `attribute_id` = @category_active_id AND entity_id = :cat_id",array(
171
+ ':cat_id'=> $category->getId()
172
+ ));
173
+ }
174
+ }
175
+ $position++;
176
+ }
177
+
178
179
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
180
$processes->walk('reindexAll');
185
186
}
187
}
188
+
189
+ unset($db_res);
190
echo 'Import Completed';
191
Mage::log("Import Completed",null,$logFileName);
192
app/code/community/Capacitywebsolutions/Importproduct/Model/System/Config/Categoryactivity.php CHANGED
File without changes
app/code/community/Capacitywebsolutions/Importproduct/Model/System/Config/Defaulttax.php ADDED
@@ -0,0 +1,19 @@
1
+ <?php
2
+
3
+ class Capacitywebsolutions_Importproduct_Model_System_Config_Defaulttax
4
+ {
5
+ public function toOptionArray()
6
+ {
7
+ $collection = Mage::getModel('tax/class')->getCollection();
8
+ $paramsArray = array();
9
+ $paramsArray['0'] = 'None';
10
+
11
+ foreach ($collection as $product) {
12
+ $tax = $product->getData();
13
+ $paramsArray[$tax['class_id']] = $tax['class_name'];
14
+ }
15
+
16
+ return $paramsArray;
17
+ }
18
+ }
19
+ ?>
app/code/community/Capacitywebsolutions/Importproduct/Model/System/Config/Imageimport.php CHANGED
File without changes
app/code/community/Capacitywebsolutions/Importproduct/Model/System/Config/Lockproddetails.php CHANGED
File without changes
app/code/community/Capacitywebsolutions/Importproduct/etc/config.xml CHANGED
@@ -6,6 +6,24 @@
6
</Capacitywebsolutions_Importproduct>
7
</modules>
8
<global>
9
<models>
10
<catalog>
11
<rewrite>
@@ -37,8 +55,9 @@
37
<use>core_read</use>
38
</connection>
39
</importproduct_setup_read>
40
- </resources>
41
</global>
42
<adminhtml>
43
<acl>
44
<resources>
6
</Capacitywebsolutions_Importproduct>
7
</modules>
8
<global>
9
+ <events>
10
+ <controller_action_predispatch_adminhtml_profile_batchfinish>
11
+ <observers>
12
+ <Capacitywebsolutions_Importproduct>
13
+ <class>Capacitywebsolutions_Importproduct/Observer</class>
14
+ <method>sortCategory</method>
15
+ </Capacitywebsolutions_Importproduct>
16
+ </observers>
17
+ </controller_action_predispatch_adminhtml_profile_batchfinish>
18
+ <controller_action_postdispatch_adminhtml_profile_batchFinish>
19
+ <observers>
20
+ <Capacitywebsolutions_Importproduct>
21
+ <class>Capacitywebsolutions_Importproduct/Observer</class>
22
+ <method>sortCategory</method>
23
+ </Capacitywebsolutions_Importproduct>
24
+ </observers>
25
+ </controller_action_postdispatch_adminhtml_profile_batchFinish>
26
+ </events>
27
<models>
28
<catalog>
29
<rewrite>
55
<use>core_read</use>
56
</connection>
57
</importproduct_setup_read>
58
+ </resources>
59
</global>
60
+
61
<adminhtml>
62
<acl>
63
<resources>
app/code/community/Capacitywebsolutions/Importproduct/etc/system.xml CHANGED
@@ -46,6 +46,15 @@
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>
46
<show_in_website>1</show_in_website>
47
<show_in_store>1</show_in_store>
48
</import_images>
49
+ <default_tax translate="label">
50
+ <label>Default tax for product</label>
51
+ <frontend_type>select</frontend_type>
52
+ <source_model>importproduct/system_config_defaulttax</source_model>
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
+ </default_tax>
58
</fields>
59
</importprod>
60
</groups>
app/code/community/Capacitywebsolutions/Importproduct/sql/importproduct_setup/mysql4-install-0.1.0.php CHANGED
@@ -8,10 +8,10 @@
8
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
9
10
$installer->addAttribute('catalog_category', 'unspsc', array(
11
- 'type' => 'varchar',
12
- 'label' => 'unspsc',
13
- 'input' => 'text',
14
- 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
15
'visible' => true,
16
'required' => false,
17
'user_defined' => false,
@@ -29,7 +29,11 @@
29
$attributeId = $installer->getAttributeId($entityTypeId, 'unspsc');
30
31
$installer->run("
32
- INSERT INTO `{$installer->getTable('catalog_category_entity_varchar')}`
33
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
34
SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
35
FROM `{$installer->getTable('catalog_category_entity')}`;
@@ -44,6 +48,12 @@
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();
8
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
9
10
$installer->addAttribute('catalog_category', 'unspsc', array(
11
+ 'type' => 'varchar',
12
+ 'label' => 'unspsc',
13
+ 'input' => 'text',
14
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
15
'visible' => true,
16
'required' => false,
17
'user_defined' => false,
29
$attributeId = $installer->getAttributeId($entityTypeId, 'unspsc');
30
31
$installer->run("
32
+
33
+ DROP TABLE IF EXISTS `iceimport_imported_product_ids`;
34
+ DROP TABLE IF EXISTS `{$installer->getTable('capacity_product_image_queue')}`;
35
+
36
+ INSERT IGNORE INTO `{$installer->getTable('catalog_category_entity_varchar')}`
37
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
38
SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
39
FROM `{$installer->getTable('catalog_category_entity')}`;
48
UNIQUE KEY (`entity_id`, `image_url`),
49
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
50
)ENGINE=InnoDB CHARSET=utf8 COMMENT='Table to manage product image import';
51
+
52
+ CREATE TABLE IF NOT EXISTS `iceimport_imported_product_ids` (
53
+ `product_id` int(11) NOT NULL,
54
+ `product_sku` varchar(255) DEFAULT NULL,
55
+ KEY `pi_idx` (`product_id`)
56
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
57
");
58
59
$installer->endSetup();
package.xml CHANGED
@@ -1,18 +1,24 @@
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>
1
<?xml version="1.0"?>
2
<package>
3
<name>ice_import</name>
4
+ <version>1.3.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>- Add default tax attribute.&#xD; - Add support multisites stores category mapping.&#xD;
12
+ - Removed skipped field warnings&#xD;
13
+ - Fix some bugs...etc.&#xD;
14
+ - Delete old not relevant products&#xD;
15
+ - Set is anchor category property to yes&#xD;
16
+ - Sort imported categories by name&#xD;
17
+ - Added method to set categories, subcategories without products to inactive </notes>
18
<authors><author><name>Iceshop</name><user>Iceshop</user><email>support@iceshop.nl</email></author></authors>
19
+ <date>2013-03-29</date>
20
+ <time>10:55:42</time>
21
+ <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="b14683db8316b3bfa88a663abc42e7df"/></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"/><file name="Defaulttax.php" hash="32a823c0c914348585702536732fed3e"/></dir></dir><file name="Observer.php" hash="cf7944e708df4fca8fbb218f8828b305"/></dir><dir name="sql"><dir name="importproduct_setup"><file name="mysql4-install-0.1.0.php" hash="61102e9caee6f92ebba4230d391b922d"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Capacitywebsolutions_Importproduct.xml" hash="02cb79dd6ed129ce3aa5305e1bfb4bbf"/></dir></target></contents>
22
<compatible/>
23
<dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
24
</package>