ice_import - Version 1.4.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
-Added imported products to all categories listed in import file for product including Default webshop's category

Download this release

Release Info

Developer IceShop
Extension ice_import
Version 1.4.0
Comparing to
See all releases


Code changes from version 1.3.0 to 1.4.0

app/code/community/Capacitywebsolutions/Importproduct/Model/Convert/Adapter/Product.php CHANGED
@@ -1,887 +1,907 @@
1
- <?php
2
- /**
3
- * Import Multiple Images during Product Import
4
- * Copyright ? 2010 Web Design by Capacity Web Solutions Pvt. Ltd. All Rights Reserved.
5
- * http://www.capacitywebsolutions.com
6
- */
7
-
8
-
9
- class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
10
- {
11
- protected $_categoryCache = array();
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
- //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);
63
- }
64
- $website = Mage::app()->getWebsite(trim($iceimportAttributes['websites']));
65
- $websiteId = $website->getId();
66
-
67
- // set store id
68
- if (empty($iceimportAttributes['store'])) {
69
- if (!is_null($this->getBatchParams('store'))) {
70
- $store = $this->getStoreById($this->getBatchParams('store'));
71
- } else {
72
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
73
- Mage::throwException($message);
74
- }
75
- }
76
- $store = $this->getStoreByCode($iceimportAttributes['store']);
77
- if ($store === false) {
78
- $message = Mage::helper('catalog')->__('Skip import row, store "%s" not exists', $iceimportAttributes['store']);
79
- Mage::throwException($message);
80
- }
81
- $storeId = $store->getId();
82
-
83
- // set type
84
- if (empty($iceimportAttributes['type'])) {
85
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'type');
86
- Mage::throwException($message);
87
- }
88
- $productType = $iceimportAttributes['type'];
89
-
90
- // set attribute set
91
- if (empty($iceimportAttributes['attribute_set'])) {
92
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'attribute_set');
93
- Mage::throwException($message);
94
- }
95
- $attribute_set = $iceimportAttributes['attribute_set'];
96
-
97
- // set sku
98
- if (empty($iceimportAttributes['sku'])) {
99
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
100
- Mage::throwException($message);
101
- }
102
- $sku = $iceimportAttributes['sku'];
103
-
104
- // set category, unspsc, unspsc path
105
- if (empty($iceimportAttributes['categories'])) {
106
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'categories');
107
- Mage::throwException($message);
108
- }
109
- $category = $iceimportAttributes['categories'];
110
- if (empty($iceimportAttributes['unspsc'])) {
111
- $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC not defined in store');
112
- Mage::throwException($message);
113
- }
114
- $unspsc = $iceimportAttributes['unspsc'];
115
- if (empty($iceimportAttributes['unspsc_path'])) {
116
- $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC path not defined in store');
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'])) {
124
- $isInStock = $iceimportAttributes['is_in_stock'];
125
- }
126
-
127
- // set qty
128
- $qty = 0;
129
- if (!empty($iceimportAttributes['qty'])) {
130
- $qty = $iceimportAttributes['qty'];
131
- }
132
-
133
- // set price
134
- $price = 0.00;
135
- if (!empty($iceimportAttributes['price'])) {
136
- $price = $iceimportAttributes['price'];
137
- }
138
-
139
- // set status value
140
- $statusValue = (!empty($iceimportAttributes['status']) && $iceimportAttributes['status'] == 'Enabled') ? 1 : 0;
141
- $productData['int']['status'] = $statusValue;
142
-
143
- // set visibility value
144
- $visibilityValue = 1;
145
- if (!empty($iceimportAttributes['visibility'])) {
146
- switch ($iceimportAttributes['visibility']) {
147
- case 'Not Visible Individually':
148
- $visibilityValue = 1;
149
- break;
150
- case 'Catalog':
151
- $visibilityValue = 2;
152
- break;
153
- case 'Search':
154
- $visibilityValue = 3;
155
- break;
156
- case 'Catalog, Search':
157
- $visibilityValue = 4;
158
- break;
159
- }
160
- }
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
170
- $initAttributes = "
171
- SELECT @product_entity_type_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
172
- entity_type_code = 'catalog_product';
173
- SELECT @category_entity_type_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
174
- entity_type_code = 'catalog_category';
175
- SELECT @attribute_set_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
176
- entity_type_code = 'catalog_product';
177
- SELECT @stock_id := `stock_id` FROM `" . $this->_tablePrefix . "cataloginventory_stock` WHERE
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;
185
-
186
- SELECT @unspcs_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
187
- `attribute_code` = 'unspsc' AND entity_type_id = @category_entity_type_id;
188
- SELECT @category_name_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
189
- `attribute_code` = 'name' AND entity_type_id = @category_entity_type_id;
190
- SELECT @category_active_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
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(
202
- ':sku' => $sku
203
- ));
204
- $productId = $prodIdFetch['entity_id'];
205
-
206
- if (!empty($productId)) {
207
- // check import type (Import only price & qty or all product info)
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);
225
- $categoryId = $this->_addCategories($category, $storeId, $unspsc, $unspscPath, $categoriesToActiveConf);
226
-
227
- // get url key
228
- $url = '';
229
- if (!empty($productData['varchar']['name'])) {
230
- $preUrl = explode(' ', strtolower($productData['varchar']['name']));
231
- $url = implode('-', $preUrl) . '-' . $iceimportAttributes['store'];
232
- }
233
- $productData['varchar']['url_key'] = $url;
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);
241
- }
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));
302
- // get product ID
303
- $prodFetch = $this->_connRes->fetchRow('SELECT @product_id AS prod_id');
304
- $productId = $prodFetch['prod_id'];
305
- } else {
306
- $coreSaveSQL .= "SELECT @product_id := " . (int)$productId . "; ";
307
- }
308
-
309
- $bindArray[':store_id'] = $storeId;
310
- foreach ($entityData as $type => $typeAttributes) {
311
- if ($type != 'spec') {
312
- $tailCoreSaveSQL = '';
313
- $attributesInit = '';
314
- if (!empty($typeAttributes)) {
315
- $tailCoreSaveSQL .= "
316
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_" . $type . "` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
317
- foreach ($typeAttributes as $attribute => $value) {
318
- $attributesInit .= "
319
- SELECT @" . $attribute . "_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
320
- `attribute_code` = '" . $attribute . "' AND entity_type_id = @product_entity_type_id;
321
- ";
322
- $tailCoreSaveSQL .= "
323
- (@product_entity_type_id, @" . $attribute . "_id, 0, @product_id, :" . $attribute . " ),
324
- (@product_entity_type_id, @" . $attribute . "_id, :store_id, @product_id, :" . $attribute . " ), ";
325
- $bindArray[':' . $attribute] = $value;
326
- }
327
- $tailCoreSaveSQL = substr($tailCoreSaveSQL, 0, -2);
328
- $tailCoreSaveSQL .= "
329
- ON DUPLICATE KEY UPDATE
330
- `value` = VALUES (`value`);
331
- ";
332
- }
333
- $coreSaveSQL .= $attributesInit . $tailCoreSaveSQL;
334
- } else {
335
- foreach ($typeAttributes as $attribute => $attributeData) {
336
- $prod_id_field = $attributeData['prod_id_field'];
337
- $table = $attributeData['table'];
338
- $field = $attributeData['field'];
339
- $value = $attributeData['value'];
340
- if (!empty($table) && !empty($field)) {
341
- $coreSaveSQL .= "
342
- UPDATE `" . $this->_tablePrefix . $table . "` SET `" . $field . "` = :" . $attribute . " WHERE `" . $prod_id_field . "` = @product_id;
343
- ";
344
- $bindArray[':' . $attribute] = $value;
345
- }
346
- }
347
- }
348
- }
349
-
350
- // categories
351
- $coreSaveSQL .= "
352
- INSERT INTO `" . $this->_tablePrefix . "catalog_category_product` (`category_id`, `product_id`, `position`) VALUES
353
- (" . (int)$categoryId . ", @product_id, 1) ON DUPLICATE KEY UPDATE `position` = 1;
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) {
365
-
366
- if (!$productId) {
367
- $message = Mage::helper('catalog')->__('Skip import row, product_id for product "%s" not defined ', $sku);
368
- Mage::throwException($message);
369
- }
370
-
371
- $stockSaveSQL = "
372
- INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_item` (`product_id`, `stock_id`, `qty`, `is_in_stock`) VALUES
373
- (:product_id, @stock_id, :qty,1)
374
- ON DUPLICATE KEY UPDATE
375
- `product_id` = :product_id,
376
- `stock_id` = @stock_id,
377
- `qty` = :qty,
378
- `is_in_stock` = :is_in_stock;
379
-
380
- INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_status` (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`) VALUES
381
- (:product_id, :webisteId, @stock_id, :qty, 1)
382
- ON DUPLICATE KEY UPDATE
383
- `product_id` = :product_id,
384
- `website_id` = :webisteId,
385
- `stock_id` = @stock_id,
386
- `qty` = :qty,
387
- `stock_status` = :is_in_stock;
388
-
389
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_website` (`product_id`, `website_id`) VALUES
390
- (:product_id, :webisteId)
391
- ON DUPLICATE KEY UPDATE
392
- `product_id` = :product_id,
393
- `website_id` = :webisteId;
394
-
395
- INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`) VALUES
396
- (@product_entity_type_id, @price_id, 0, :product_id, :price)
397
- ON DUPLICATE KEY UPDATE
398
- `entity_type_id` = @product_entity_type_id,
399
- `attribute_id` = @price_id,
400
- `store_id` = 0,
401
- `entity_id` = :product_id,
402
- `value` = :price;
403
- ";
404
-
405
- $this->_connRes->query($stockSaveSQL, array(
406
- ':webisteId' => $website,
407
- ':product_id' => $productId,
408
- ':price' => $price,
409
- ':qty' => $qty,
410
- ':is_in_stock' => $isInStock
411
- ));
412
-
413
- }
414
-
415
- protected function _addImageToQueue ($productId =false, $productImageUrl) {
416
- $productImageUrl = trim($productImageUrl);
417
- if ($productId && !empty($productImageUrl)) {
418
- // add image if not exists to queue
419
- $this->_connRes->query(" INSERT IGNORE INTO `" . $this->_tablePrefix . "capacity_product_image_queue` (`entity_id`, `image_url` ) VALUES
420
- (:product_id, :image_url)
421
- ", array(':product_id' => $productId,
422
- ':image_url' => $productImageUrl));
423
- }
424
- }
425
-
426
- public function getImageQueue(){
427
- return $this->_connRes->fetchAll("SELECT `queue_id`, `entity_id`, `image_url` FROM `" . $this->_tablePrefix . "capacity_product_image_queue`
428
- WHERE `is_downloaded` = 0
429
- ");
430
- }
431
-
432
- public function setImageAsDownloaded($queueId =false){
433
- if ($queueId) {
434
- $this->_connRes->query("UPDATE `" . $this->_tablePrefix . "capacity_product_image_queue` SET is_downloaded = 1
435
- WHERE queue_id = :queue_id", array(':queue_id' => $queueId));
436
- }
437
- }
438
-
439
- protected function _addCategories($categories, $storeId, $unspsc, $unspscPath, $categoryActive = 1) {
440
-
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--) {
511
- $category = $categoryMergedArray[$i];
512
- $checkCategoryId = $this->_getCategoryIdByUnspsc($category['unspsc']);
513
- if ($checkCategoryId != null) {
514
- $categoryId = $this->_buildCategoryTree($checkCategoryId, $storeId, $categoryCreateArray, $categoryActive);
515
- break;
516
- } else {
517
- $categoryCreateArray[] = $category;
518
- }
519
- }
520
- return $categoryId;
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();
551
- } else {
552
- $categoryId = $this->_connRes->fetchRow("SELECT entity_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
553
- `value` = :unspsc AND attribute_id = @unspcs_id", array(':unspsc' => $unspcs));
554
- return ($categoryId['entity_id']) ? $categoryId['entity_id'] : null;
555
- }
556
- }
557
-
558
- protected function _buildCategoryTree($parrentCategoryId, $storeId, $pathArray, $categoryActive = 0) {
559
- for ($i = count($pathArray) -1; $i >= 0; $i--) {
560
- $category = $pathArray[$i];
561
- $parrentCategoryId = $this->_createCategory($parrentCategoryId, $category['unspsc'], $storeId, $category['name'], $categoryActive);
562
- }
563
-
564
- return $parrentCategoryId;
565
- }
566
-
567
- protected function _createCategory($parrentId, $unspsc, $storeId, $name, $categoryActive = 0) {
568
-
569
- $addCategory = "
570
- SELECT @tPath := `path`, @tLevel := `level` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE `entity_id` = :parrent_id;
571
- SET @tLevel = @tLevel +1;
572
-
573
- SET @path := CONCAT(@tPath, '/',(SELECT MAX(entity_id) FROM `" . $this->_tablePrefix . "catalog_category_entity`) +1 );
574
-
575
- INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity` (`entity_type_id`, `attribute_set_id`,
576
- `parent_id`, `created_at`,
577
- `path`, `position`,
578
- `level`, `children_count`)
579
- VALUES
580
- (@category_entity_type_id, 0, :parrent_id, NOW(), @path, 1, @tLevel, 0);
581
-
582
- SELECT @catId := LAST_INSERT_ID();
583
-
584
- UPDATE `" . $this->_tablePrefix . "catalog_category_entity` SET children_count = children_count +1 WHERE entity_id = :parrent_id;
585
-
586
- INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`,
587
- `store_id`, `entity_id`, `value`)
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
-
596
- INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`,
597
- `store_id`, `entity_id`, `value`)
598
- VALUES
599
- (@category_entity_type_id, @category_name_id, 0, @catId, :category_name),
600
- (@category_entity_type_id, @category_name_id, :store, @catId, :category_name),
601
- (@category_entity_type_id, @unspcs_id, 0, @catId, :unspsc_val),
602
- (@category_entity_type_id, @unspcs_id, :store, @catId, :unspsc_val);
603
- ";
604
-
605
- $this->_connRes->query($addCategory, array(
606
- ':store' => $storeId,
607
- ':parrent_id' => $parrentId,
608
- ':category_name' => $name,
609
- ':unspsc_val' => $unspsc,
610
- ':category_active' => (int)$categoryActive
611
- ));
612
-
613
- $categoryIdFetch = $this->_connRes->fetchRow('SELECT @catId AS category_id');
614
-
615
- return $categoryIdFetch['category_id'];
616
- }
617
-
618
- protected function _mapAttributes(array $importData) {
619
-
620
- // map iceimport attributes, skip some attributes
621
- $iceAttributes = array();
622
- foreach ($importData as $attribute => $value) {
623
- // map iceimport attributes
624
- if ($attribute == 'type' ||
625
- $attribute == 'sku' ||
626
- $attribute == 'attribute_set' ||
627
- $attribute == 'categories' ||
628
- $attribute == 'unspsc' ||
629
- $attribute == 'price' ||
630
- $attribute == 'qty' ||
631
- $attribute == 'status' ||
632
- $attribute == 'visibility' ||
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' ||
646
- $attribute == 'leader_categories' ||
647
- $attribute == 'leader_store' ||
648
- $attribute == 'sprice' ||
649
- $attribute == 'euprice' ||
650
- $attribute == 'icecat_product_id' ||
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' ||
658
- $attribute == 'tax1' ||
659
- $attribute == 'tax2' ||
660
- $attribute == 'tax3' ||
661
- $attribute == 'tax4' ||
662
- $attribute == 'min_quantity' ||
663
- $attribute == 'loms' ||
664
- $attribute == 'image_label' ||
665
- $attribute == 'links_title' ||
666
- $attribute == 'small_image_label' ||
667
- $attribute == 'tax_rate' ||
668
- $attribute == 'gallery' ||
669
- $attribute == 'weight_type' ||
670
- $attribute == 'sku_type' ||
671
- $attribute == 'manage_stock' ||
672
- $attribute == 'minimal_price' ||
673
- $attribute == 'required_options' ||
674
- $attribute == 'samples_title' ||
675
- $attribute == 'shipment_type' ||
676
- $attribute == 'url_path' ||
677
- $attribute == 'recurring_profile' ||
678
- $attribute == 'product_keys') {
679
-
680
- unset($importData[$attribute]);
681
-
682
- }
683
-
684
- }
685
-
686
- // map default attributes
687
- $productData = array();
688
- foreach($this->_getDefaultAttributesList() as $backendType => $attributesList) {
689
- if ($backendType != 'spec') {
690
- foreach($attributesList as $attribute) {
691
- if (isset($importData[$attribute])) {
692
- $productData[$backendType][$attribute] = $importData[$attribute];
693
- unset($importData[$attribute]);
694
- }
695
- }
696
- } else {
697
- foreach($attributesList as $attributeCode => $attributeSpecs) {
698
- if (isset($importData[$attributeCode])) {
699
- $attributeSpecs['value'] = $importData[$attributeCode];
700
- $productData[$backendType][$attributeCode] = $attributeSpecs;
701
- unset($importData[$attributeCode]);
702
- }
703
- }
704
- }
705
- }
706
-
707
- // map custom attributes
708
- if (!empty($importData)) {
709
- foreach ($importData as $attributeCode => $value) {
710
- $backendTypeFetch = $this->_connRes->fetchRow("SELECT backend_type FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = :code", array(':code' => $attributeCode));
711
- $backendType = $backendTypeFetch['backend_type'];
712
- if ($backendType != 'static' && !empty($backendType)) {
713
- $productData[$backendType][$attributeCode] = $value;
714
- unset($importData[$attributeCode]);
715
- }
716
- }
717
- }
718
-
719
- $failedAttributes = array();
720
- if (count($importData) > 0) {
721
- $failedAttributes = array_keys($importData);
722
- }
723
-
724
- return array(
725
- 'iceimportAttributes' => $iceAttributes,
726
- 'productData' => $productData,
727
- 'failedAttributes' => $failedAttributes
728
- );
729
-
730
- }
731
-
732
- protected function _getDefaultAttributesList () {
733
- return array(
734
- 'varchar' => array(
735
- 'gift_message_available',
736
- 'custom_design',
737
- 'msrp_display_actual_price_type',
738
- 'msrp_enabled',
739
- 'options_container',
740
- 'page_layout',
741
- 'mpn',
742
- 'brand_name',
743
- 'name',
744
- 'url_key',
745
- 'meta_description',
746
- 'meta_title'
747
- ),
748
- 'int' => array(
749
- 'enable_googlecheckout',
750
- 'is_recurring',
751
- 'links_purchased_separately',
752
- 'links_exist',
753
- 'status',
754
- 'visibility',
755
- 'tax_class_id',
756
- 'color',
757
- 'price_view',
758
- 'manufacturer'
759
- ),
760
- 'text' => array(
761
- 'recurring_profile',
762
- 'description',
763
- 'custom_layout_update',
764
- 'meta_keyword',
765
- 'short_description'
766
- ),
767
- 'decimal' => array(
768
- 'cost',
769
- 'group_price',
770
- 'weight',
771
- 'special_price',
772
- 'msrp'
773
- ),
774
- 'datetime' => array(
775
- 'custom_design_from',
776
- 'custom_design_to',
777
- 'news_from_date',
778
- 'news_to_date',
779
- 'special_from_date',
780
- 'special_to_date'
781
- ),
782
- 'spec' => array(
783
- 'is_qty_decimal' => array(
784
- 'prod_id_field' => 'product_id',
785
- 'table' => 'cataloginventory_stock_item',
786
- 'field' => 'is_qty_decimal'
787
- ),
788
- 'use_config_min_qty' => array(
789
- 'prod_id_field' => 'product_id',
790
- 'table' => 'cataloginventory_stock_item',
791
- 'field' => 'use_config_min_qty'
792
- ),
793
- 'use_config_min_sale_qty' => array(
794
- 'prod_id_field' => 'product_id',
795
- 'table' => 'cataloginventory_stock_item',
796
- 'field' => 'use_config_min_sale_qty'
797
- ),
798
- 'use_config_max_sale_qty' => array(
799
- 'prod_id_field' => 'product_id',
800
- 'table' => 'cataloginventory_stock_item',
801
- 'field' => 'use_config_max_sale_qty'
802
- ),
803
- 'use_config_manage_stock' => array(
804
- 'prod_id_field' => 'product_id',
805
- 'table' => 'cataloginventory_stock_item',
806
- 'field' => 'use_config_manage_stock'
807
- ),
808
- 'is_decimal_divided' => array(
809
- 'prod_id_field' => 'product_id',
810
- 'table' => 'cataloginventory_stock_item',
811
- 'field' => 'is_decimal_divided'
812
- ),
813
- 'use_config_backorders' => array(
814
- 'prod_id_field' => 'product_id',
815
- 'table' => 'cataloginventory_stock_item',
816
- 'field' => 'use_config_backorders'
817
- ),
818
- 'use_config_notify_stock_qty' => array(
819
- 'prod_id_field' => 'product_id',
820
- 'table' => 'cataloginventory_stock_item',
821
- 'field' => 'use_config_notify_stock_qty'
822
- ),
823
- 'max_sale_qty' => array(
824
- 'prod_id_field' => 'product_id',
825
- 'table' => 'cataloginventory_stock_item',
826
- 'field' => 'max_sale_qty'
827
- ),
828
- 'min_sale_qty' => array(
829
- 'prod_id_field' => 'product_id',
830
- 'table' => 'cataloginventory_stock_item',
831
- 'field' => 'min_sale_qty'
832
- ),
833
- 'notify_stock_qty' => array(
834
- 'prod_id_field' => 'product_id',
835
- 'table' => 'cataloginventory_stock_item',
836
- 'field' => 'notify_stock_qty'
837
- ),
838
- 'backorders' => array(
839
- 'prod_id_field' => 'product_id',
840
- 'table' => 'cataloginventory_stock_item',
841
- 'field' => 'backorders'
842
- ),
843
- 'created_at' => array(
844
- 'prod_id_field' => 'entity_id',
845
- 'table' => 'catalog_product_entity',
846
- 'field' => 'created_at'
847
- ),
848
- 'min_qty' => array(
849
- 'prod_id_field' => 'product_id',
850
- 'table' => 'cataloginventory_stock_item',
851
- 'field' => 'min_qty'
852
- ),
853
- 'updated_at' => array(
854
- 'prod_id_field' => 'entity_id',
855
- 'table' => 'catalog_product_entity',
856
- 'field' => 'updated_at'
857
- )
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
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Import Multiple Images during Product Import
4
+ * Copyright ? 2010 Web Design by Capacity Web Solutions Pvt. Ltd. All Rights Reserved.
5
+ * http://www.capacitywebsolutions.com
6
+ */
7
+
8
+
9
+ class Capacitywebsolutions_Importproduct_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
10
+ {
11
+ protected $_categoryCache = array();
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
+ //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);
63
+ }
64
+ $website = Mage::app()->getWebsite(trim($iceimportAttributes['websites']));
65
+ $websiteId = $website->getId();
66
+
67
+ // set store id
68
+ if (empty($iceimportAttributes['store'])) {
69
+ if (!is_null($this->getBatchParams('store'))) {
70
+ $store = $this->getStoreById($this->getBatchParams('store'));
71
+ } else {
72
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
73
+ Mage::throwException($message);
74
+ }
75
+ }
76
+ $store = $this->getStoreByCode($iceimportAttributes['store']);
77
+ if ($store === false) {
78
+ $message = Mage::helper('catalog')->__('Skip import row, store "%s" not exists', $iceimportAttributes['store']);
79
+ Mage::throwException($message);
80
+ }
81
+ $storeId = $store->getId();
82
+
83
+ // set type
84
+ if (empty($iceimportAttributes['type'])) {
85
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'type');
86
+ Mage::throwException($message);
87
+ }
88
+ $productType = $iceimportAttributes['type'];
89
+
90
+ // set attribute set
91
+ if (empty($iceimportAttributes['attribute_set'])) {
92
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'attribute_set');
93
+ Mage::throwException($message);
94
+ }
95
+ $attribute_set = $iceimportAttributes['attribute_set'];
96
+
97
+ // set sku
98
+ if (empty($iceimportAttributes['sku'])) {
99
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
100
+ Mage::throwException($message);
101
+ }
102
+ $sku = $iceimportAttributes['sku'];
103
+
104
+ // set category, unspsc, unspsc path
105
+ if (empty($iceimportAttributes['categories'])) {
106
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'categories');
107
+ Mage::throwException($message);
108
+ }
109
+ $category = $iceimportAttributes['categories'];
110
+ if (empty($iceimportAttributes['unspsc'])) {
111
+ $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC not defined in store');
112
+ Mage::throwException($message);
113
+ }
114
+ $unspsc = $iceimportAttributes['unspsc'];
115
+ if (empty($iceimportAttributes['unspsc_path'])) {
116
+ $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC path not defined in store');
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'])) {
124
+ $isInStock = $iceimportAttributes['is_in_stock'];
125
+ }
126
+
127
+ // set qty
128
+ $qty = 0;
129
+ if (!empty($iceimportAttributes['qty'])) {
130
+ $qty = $iceimportAttributes['qty'];
131
+ }
132
+
133
+ // set price
134
+ $price = 0.00;
135
+ if (!empty($iceimportAttributes['price'])) {
136
+ $price = $iceimportAttributes['price'];
137
+ }
138
+
139
+ // set status value
140
+ $statusValue = (!empty($iceimportAttributes['status']) && $iceimportAttributes['status'] == 'Enabled') ? 1 : 0;
141
+ $productData['int']['status'] = $statusValue;
142
+
143
+ // set visibility value
144
+ $visibilityValue = 1;
145
+ if (!empty($iceimportAttributes['visibility'])) {
146
+ switch ($iceimportAttributes['visibility']) {
147
+ case 'Not Visible Individually':
148
+ $visibilityValue = 1;
149
+ break;
150
+ case 'Catalog':
151
+ $visibilityValue = 2;
152
+ break;
153
+ case 'Search':
154
+ $visibilityValue = 3;
155
+ break;
156
+ case 'Catalog, Search':
157
+ $visibilityValue = 4;
158
+ break;
159
+ }
160
+ }
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
170
+ $initAttributes = "
171
+ SELECT @product_entity_type_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
172
+ entity_type_code = 'catalog_product';
173
+ SELECT @category_entity_type_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
174
+ entity_type_code = 'catalog_category';
175
+ SELECT @attribute_set_id := `entity_type_id` FROM `" . $this->_tablePrefix . "eav_entity_type` WHERE
176
+ entity_type_code = 'catalog_product';
177
+ SELECT @stock_id := `stock_id` FROM `" . $this->_tablePrefix . "cataloginventory_stock` WHERE
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;
185
+
186
+ SELECT @unspcs_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
187
+ `attribute_code` = 'unspsc' AND entity_type_id = @category_entity_type_id;
188
+ SELECT @category_name_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
189
+ `attribute_code` = 'name' AND entity_type_id = @category_entity_type_id;
190
+ SELECT @category_active_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
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(
202
+ ':sku' => $sku
203
+ ));
204
+ $productId = $prodIdFetch['entity_id'];
205
+
206
+ if (!empty($productId)) {
207
+ // check import type (Import only price & qty or all product info)
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);
225
+ $categoryIds = $this->_addCategories($category, $storeId, $unspsc, $unspscPath, $categoriesToActiveConf);
226
+
227
+ // get url key
228
+ $url = '';
229
+ if (!empty($productData['varchar']['name'])) {
230
+ $preUrl = explode(' ', strtolower($productData['varchar']['name']));
231
+ $url = implode('-', $preUrl) . '-' . $iceimportAttributes['store'];
232
+ }
233
+ $productData['varchar']['url_key'] = $url;
234
+
235
+ // if new product then ovewrride product id from null to id
236
+ $productId = $this->_coreSave($productData, $productId, $storeId, $sku, $categoryIds);
237
+
238
+ // add product image to queue
239
+ if (Mage::getStoreConfig('importprod_root/importprod/import_images')) {
240
+ $this->_addImageToQueue($productId, $productImage);
241
+ }
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, $categoryIds) {
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));
302
+ // get product ID
303
+ $prodFetch = $this->_connRes->fetchRow('SELECT @product_id AS prod_id');
304
+ $productId = $prodFetch['prod_id'];
305
+ } else {
306
+ $coreSaveSQL .= "SELECT @product_id := " . (int)$productId . "; ";
307
+ }
308
+
309
+ $bindArray[':store_id'] = $storeId;
310
+ foreach ($entityData as $type => $typeAttributes) {
311
+ if ($type != 'spec') {
312
+ $tailCoreSaveSQL = '';
313
+ $attributesInit = '';
314
+ if (!empty($typeAttributes)) {
315
+ $tailCoreSaveSQL .= "
316
+ INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_" . $type . "` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
317
+ foreach ($typeAttributes as $attribute => $value) {
318
+ $attributesInit .= "
319
+ SELECT @" . $attribute . "_id := `attribute_id` FROM `" . $this->_tablePrefix . "eav_attribute` WHERE
320
+ `attribute_code` = '" . $attribute . "' AND entity_type_id = @product_entity_type_id;
321
+ ";
322
+ $tailCoreSaveSQL .= "
323
+ (@product_entity_type_id, @" . $attribute . "_id, 0, @product_id, :" . $attribute . " ),
324
+ (@product_entity_type_id, @" . $attribute . "_id, :store_id, @product_id, :" . $attribute . " ), ";
325
+ $bindArray[':' . $attribute] = $value;
326
+ }
327
+ $tailCoreSaveSQL = substr($tailCoreSaveSQL, 0, -2);
328
+ $tailCoreSaveSQL .= "
329
+ ON DUPLICATE KEY UPDATE
330
+ `value` = VALUES (`value`);
331
+ ";
332
+ }
333
+ $coreSaveSQL .= $attributesInit . $tailCoreSaveSQL;
334
+ } else {
335
+ foreach ($typeAttributes as $attribute => $attributeData) {
336
+ $prod_id_field = $attributeData['prod_id_field'];
337
+ $table = $attributeData['table'];
338
+ $field = $attributeData['field'];
339
+ $value = $attributeData['value'];
340
+ if (!empty($table) && !empty($field)) {
341
+ $coreSaveSQL .= "
342
+ UPDATE `" . $this->_tablePrefix . $table . "` SET `" . $field . "` = :" . $attribute . " WHERE `" . $prod_id_field . "` = @product_id;
343
+ ";
344
+ $bindArray[':' . $attribute] = $value;
345
+ }
346
+ }
347
+ }
348
+ }
349
+
350
+
351
+ // categories
352
+ $coreSaveSQL .= "INSERT INTO `" . $this->_tablePrefix . "catalog_category_product` (`category_id`, `product_id`, `position`) VALUES ";
353
+ $counter = 1;
354
+ $categoryIds[] = Mage::app()->getStore(1)->getRootCategoryId();
355
+ foreach ($categoryIds as $categoryId) {
356
+ if ($counter < count($categoryIds)) {
357
+ $coreSaveSQL .= " (" . (int)$categoryId . ", @product_id, 1) , ";
358
+ } else if ($counter == count($categoryIds)) {
359
+ $coreSaveSQL .= " (" . (int)$categoryId . ", @product_id, 1) ON DUPLICATE KEY UPDATE `position` = 1 ";
360
+ }
361
+ $counter++;
362
+ }
363
+ try{
364
+ $query = $this->_connRes->query($coreSaveSQL, $bindArray);
365
+
366
+ /*$newCategories = $categoryIds;
367
+ $newCategories[] = Mage::app()->getStore(1)->getRootCategoryId();
368
+ $product = Mage::getModel('catalog/product')->load($productId);
369
+ $product->setCategoryIds(
370
+ array_merge($product->getCategoryIds(), $newCategories)
371
+ );
372
+ echo ' 367 ';
373
+ $product->save();*/
374
+ } catch(Exception $e) {
375
+ echo $e->getMessage();
376
+ }
377
+ return $productId;
378
+ }
379
+
380
+ protected function _corePriceStock($website =0, $productId =false, $price =0.00, $qty =0.00, $sku =false, $isInStock =0) {
381
+
382
+ if (!$productId) {
383
+ $message = Mage::helper('catalog')->__('Skip import row, product_id for product "%s" not defined ', $sku);
384
+ Mage::throwException($message);
385
+ }
386
+
387
+ $stockSaveSQL = "
388
+ INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_item` (`product_id`, `stock_id`, `qty`, `is_in_stock`) VALUES
389
+ (:product_id, @stock_id, :qty,1)
390
+ ON DUPLICATE KEY UPDATE
391
+ `product_id` = :product_id,
392
+ `stock_id` = @stock_id,
393
+ `qty` = :qty,
394
+ `is_in_stock` = :is_in_stock;
395
+
396
+ INSERT INTO `" . $this->_tablePrefix . "cataloginventory_stock_status` (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`) VALUES
397
+ (:product_id, :webisteId, @stock_id, :qty, 1)
398
+ ON DUPLICATE KEY UPDATE
399
+ `product_id` = :product_id,
400
+ `website_id` = :webisteId,
401
+ `stock_id` = @stock_id,
402
+ `qty` = :qty,
403
+ `stock_status` = :is_in_stock;
404
+
405
+ INSERT INTO `" . $this->_tablePrefix . "catalog_product_website` (`product_id`, `website_id`) VALUES
406
+ (:product_id, :webisteId)
407
+ ON DUPLICATE KEY UPDATE
408
+ `product_id` = :product_id,
409
+ `website_id` = :webisteId;
410
+
411
+ INSERT INTO `" . $this->_tablePrefix . "catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`) VALUES
412
+ (@product_entity_type_id, @price_id, 0, :product_id, :price)
413
+ ON DUPLICATE KEY UPDATE
414
+ `entity_type_id` = @product_entity_type_id,
415
+ `attribute_id` = @price_id,
416
+ `store_id` = 0,
417
+ `entity_id` = :product_id,
418
+ `value` = :price;
419
+ ";
420
+
421
+ $this->_connRes->query($stockSaveSQL, array(
422
+ ':webisteId' => $website,
423
+ ':product_id' => $productId,
424
+ ':price' => $price,
425
+ ':qty' => $qty,
426
+ ':is_in_stock' => $isInStock
427
+ ));
428
+
429
+ }
430
+
431
+ protected function _addImageToQueue ($productId =false, $productImageUrl) {
432
+ $productImageUrl = trim($productImageUrl);
433
+ if ($productId && !empty($productImageUrl)) {
434
+ // add image if not exists to queue
435
+ $this->_connRes->query(" INSERT IGNORE INTO `" . $this->_tablePrefix . "capacity_product_image_queue` (`entity_id`, `image_url` ) VALUES
436
+ (:product_id, :image_url)
437
+ ", array(':product_id' => $productId,
438
+ ':image_url' => $productImageUrl));
439
+ }
440
+ }
441
+
442
+ public function getImageQueue(){
443
+ return $this->_connRes->fetchAll("SELECT `queue_id`, `entity_id`, `image_url` FROM `" . $this->_tablePrefix . "capacity_product_image_queue`
444
+ WHERE `is_downloaded` = 0
445
+ ");
446
+ }
447
+
448
+ public function setImageAsDownloaded($queueId =false){
449
+ if ($queueId) {
450
+ $this->_connRes->query("UPDATE `" . $this->_tablePrefix . "capacity_product_image_queue` SET is_downloaded = 1
451
+ WHERE queue_id = :queue_id", array(':queue_id' => $queueId));
452
+ }
453
+ }
454
+
455
+ protected function _addCategories($categories, $storeId, $unspsc, $unspscPath, $categoryActive = 1) {
456
+
457
+ // check if product exists
458
+ $categoryId = $this->_getCategoryIdByUnspsc($unspsc);
459
+ $categoryIds = array();
460
+
461
+ if (!empty($categoryId)) {
462
+ // merge categories by unspsc
463
+ $categoryMergedArray = $this->_categoryMapper($categories, $unspscPath);
464
+ foreach ($categoryMergedArray as $category) {
465
+ $categoryName = $category['name'];
466
+ $categoryUnspsc = $category['unspsc'];
467
+ $categoryTreeId = $this->_getCategoryIdByUnspsc($categoryUnspsc);
468
+ // check category name to current store
469
+ $categoryBindArray = array(
470
+ ':store_id' => $storeId,
471
+ ':category_id' => $categoryTreeId
472
+ );
473
+ $nameCheckerFetch = $this->_connRes->fetchRow("SELECT value_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
474
+ store_id = :store_id AND entity_id = :category_id AND attribute_id = @category_name_id
475
+ ", $categoryBindArray);
476
+ $nameChecker = $nameCheckerFetch['value_id'];
477
+ if (!$nameChecker) {
478
+ // add category name to current store
479
+ $categoryBindArray[':category_name'] = $categoryName;
480
+ if (!empty($categoryBindArray[':category_id'])){
481
+ $this->_connRes->query("
482
+ INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES
483
+ (@category_entity_type_id, @category_name_id, :store_id, :category_id, :category_name)
484
+ ", $categoryBindArray);
485
+ }
486
+ }
487
+ }
488
+ if (1 == $categoryActive) {
489
+ // get current path of category
490
+ $categoryPath = $this->_connRes->fetchRow("SELECT path FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE entity_id = :entity_id",
491
+ array(':entity_id' => $categoryId));
492
+ $categoryPathArray = explode('/', $categoryPath['path']);
493
+ if ($categoryPathArray) {
494
+ $activeSetter = "INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
495
+ }
496
+
497
+ $falseCounter = 0;
498
+ foreach($categoryPathArray as $categoryId) {
499
+ $category = Mage::getModel('catalog/category')->load($categoryId);
500
+ $cid = $category->getId();
501
+ if (!empty($cid)) {
502
+ if (!empty($categoryId)) {
503
+ $categoryIds[] = (int)$categoryId;
504
+ $activeSetter .= "(@category_entity_type_id, @category_active_id, :store_id, " . $categoryId . ", 1),
505
+ (@category_entity_type_id, @category_active_id, 0, " . $categoryId . ", 1), ";
506
+ } else {
507
+ $falseCounter++;
508
+ }
509
+ } else {
510
+ $falseCounter++;
511
+ }
512
+ }
513
+ $activeSetter = substr($activeSetter, 0, -2);
514
+ $activeSetter .= "
515
+ ON DUPLICATE KEY UPDATE
516
+ `value` = 1
517
+ ";
518
+ if ($falseCounter < count($categoryPathArray)) {
519
+ $this->_connRes->query($activeSetter, array(':store_id' => $storeId));
520
+ }
521
+ }
522
+ return $categoryIds;
523
+ } else {
524
+
525
+ // merge unspcs to current name in unspcs & name path's
526
+ $categoryMergedArray = $this->_categoryMapper($categories, $unspscPath);
527
+ // get max created parrent category
528
+ $categoryCreateArray = array();
529
+ for ($i = count($categoryMergedArray) -1; $i >= 0; $i--) {
530
+ $category = $categoryMergedArray[$i];
531
+ $checkCategoryId = $this->_getCategoryIdByUnspsc($category['unspsc']);
532
+ if ($checkCategoryId != null) {
533
+ $categoryId = $this->_buildCategoryTree($checkCategoryId, $storeId, $categoryCreateArray, $categoryActive);
534
+ $categoryIds[] = (int)$categoryId;
535
+ break;
536
+ } else {
537
+ $categoryCreateArray[] = $category;
538
+ }
539
+ }
540
+ return $categoryIds;
541
+ }
542
+ }
543
+
544
+ protected function _categoryMapper($categoryPath, $unspscPath) {
545
+ $nameArray = explode('/', $categoryPath);
546
+ $unspscArray = explode('/', $unspscPath);
547
+
548
+ if (count($nameArray) != count($unspscArray)) {
549
+ $message = Mage::helper('catalog')->__('Skip import row, @categories data is invaled');
550
+ Mage::throwException($message);
551
+ }
552
+
553
+ $categoryMergedArray = array(
554
+ array(
555
+ 'unspsc' => 'default_root',
556
+ 'name' => 'Default category'
557
+ )
558
+ );
559
+
560
+ for($i = 0; $i < count($unspscArray); $i++) {
561
+ $categoryMergedArray[] = array('name' =>$nameArray[$i],
562
+ 'unspsc' =>$unspscArray[$i]);
563
+ }
564
+
565
+ return $categoryMergedArray;
566
+ }
567
+
568
+ protected function _getCategoryIdByUnspsc($unspcs) {
569
+ if ($unspcs == 'default_root') {
570
+ return Mage::app()->getStore(1)->getRootCategoryId();
571
+ } else {
572
+ $categoryId = $this->_connRes->fetchRow("SELECT entity_id FROM `" . $this->_tablePrefix . "catalog_category_entity_varchar` WHERE
573
+ `value` = :unspsc AND attribute_id = @unspcs_id", array(':unspsc' => $unspcs));
574
+ return ($categoryId['entity_id']) ? $categoryId['entity_id'] : null;
575
+ }
576
+ }
577
+
578
+ protected function _buildCategoryTree($parrentCategoryId, $storeId, $pathArray, $categoryActive = 0) {
579
+ for ($i = count($pathArray) -1; $i >= 0; $i--) {
580
+ $category = $pathArray[$i];
581
+ $parrentCategoryId = $this->_createCategory($parrentCategoryId, $category['unspsc'], $storeId, $category['name'], $categoryActive);
582
+ }
583
+
584
+ return $parrentCategoryId;
585
+ }
586
+
587
+ protected function _createCategory($parrentId, $unspsc, $storeId, $name, $categoryActive = 0) {
588
+
589
+ $addCategory = "
590
+ SELECT @tPath := `path`, @tLevel := `level` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE `entity_id` = :parrent_id;
591
+ SET @tLevel = @tLevel +1;
592
+
593
+ SET @path := CONCAT(@tPath, '/',(SELECT MAX(entity_id) FROM `" . $this->_tablePrefix . "catalog_category_entity`) +1 );
594
+
595
+ INSERT INTO `" . $this->_tablePrefix . "catalog_category_entity` (`entity_type_id`, `attribute_set_id`,
596
+ `parent_id`, `created_at`,
597
+ `path`, `position`,
598
+ `level`, `children_count`)
599
+ VALUES
600
+ (@category_entity_type_id, 0, :parrent_id, NOW(), @path, 1, @tLevel, 0);
601
+
602
+ SELECT @catId := LAST_INSERT_ID();
603
+
604
+ UPDATE `" . $this->_tablePrefix . "catalog_category_entity` SET children_count = children_count +1 WHERE entity_id = :parrent_id;
605
+
606
+ INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_int` (`entity_type_id`, `attribute_id`,
607
+ `store_id`, `entity_id`, `value`)
608
+ VALUES
609
+ (@category_entity_type_id, @category_active_id, 0, @catId, :category_active),
610
+ (@category_entity_type_id, @category_active_id, :store, @catId, :category_active),
611
+ (@category_entity_type_id, @category_is_anchor_id, 0, @catId, 1),
612
+ (@category_entity_type_id, @category_is_anchor_id, :store, @catId, 1),
613
+ (@category_entity_type_id, @include_nav_bar_id, 0, @catId, 1),
614
+ (@category_entity_type_id, @include_nav_bar_id, :store, @catId, 1);
615
+
616
+ INSERT IGNORE INTO `" . $this->_tablePrefix . "catalog_category_entity_varchar` (`entity_type_id`, `attribute_id`,
617
+ `store_id`, `entity_id`, `value`)
618
+ VALUES
619
+ (@category_entity_type_id, @category_name_id, 0, @catId, :category_name),
620
+ (@category_entity_type_id, @category_name_id, :store, @catId, :category_name),
621
+ (@category_entity_type_id, @unspcs_id, 0, @catId, :unspsc_val),
622
+ (@category_entity_type_id, @unspcs_id, :store, @catId, :unspsc_val);
623
+ ";
624
+
625
+ $this->_connRes->query($addCategory, array(
626
+ ':store' => $storeId,
627
+ ':parrent_id' => $parrentId,
628
+ ':category_name' => $name,
629
+ ':unspsc_val' => $unspsc,
630
+ ':category_active' => (int)$categoryActive
631
+ ));
632
+
633
+ $categoryIdFetch = $this->_connRes->fetchRow('SELECT @catId AS category_id');
634
+
635
+ return $categoryIdFetch['category_id'];
636
+ }
637
+
638
+ protected function _mapAttributes(array $importData) {
639
+
640
+ // map iceimport attributes, skip some attributes
641
+ $iceAttributes = array();
642
+ foreach ($importData as $attribute => $value) {
643
+ // map iceimport attributes
644
+ if ($attribute == 'type' ||
645
+ $attribute == 'sku' ||
646
+ $attribute == 'attribute_set' ||
647
+ $attribute == 'categories' ||
648
+ $attribute == 'unspsc' ||
649
+ $attribute == 'price' ||
650
+ $attribute == 'qty' ||
651
+ $attribute == 'status' ||
652
+ $attribute == 'visibility' ||
653
+ $attribute == 'store' ||
654
+ $attribute == 'websites' ||
655
+ $attribute == 'is_in_stock' ||
656
+ $attribute == 'image' ||
657
+ $attribute == 'unspsc_path') {
658
+
659
+ $iceAttributes[$attribute] = $value;
660
+ unset($importData[$attribute]);
661
+
662
+ }
663
+ // skip some attributes
664
+ if ($attribute == 'supplier_product_code' ||
665
+ $attribute == 'supplier' ||
666
+ $attribute == 'leader_categories' ||
667
+ $attribute == 'leader_store' ||
668
+ $attribute == 'sprice' ||
669
+ $attribute == 'euprice' ||
670
+ $attribute == 'icecat_product_id' ||
671
+ $attribute == 'icecat_category_id' ||
672
+ $attribute == 'icecat_vendor_id' ||
673
+ $attribute == 'icecat_quality' ||
674
+ $attribute == 'icecat_url' ||
675
+ $attribute == 'icecat_thumbnail_img' ||
676
+ $attribute == 'icecat_low_res_img' ||
677
+ $attribute == 'icecat_high_res_img' ||
678
+ $attribute == 'tax1' ||
679
+ $attribute == 'tax2' ||
680
+ $attribute == 'tax3' ||
681
+ $attribute == 'tax4' ||
682
+ $attribute == 'min_quantity' ||
683
+ $attribute == 'loms' ||
684
+ $attribute == 'image_label' ||
685
+ $attribute == 'links_title' ||
686
+ $attribute == 'small_image_label' ||
687
+ $attribute == 'tax_rate' ||
688
+ $attribute == 'gallery' ||
689
+ $attribute == 'weight_type' ||
690
+ $attribute == 'sku_type' ||
691
+ $attribute == 'manage_stock' ||
692
+ $attribute == 'minimal_price' ||
693
+ $attribute == 'required_options' ||
694
+ $attribute == 'samples_title' ||
695
+ $attribute == 'shipment_type' ||
696
+ $attribute == 'url_path' ||
697
+ $attribute == 'recurring_profile' ||
698
+ $attribute == 'product_keys') {
699
+
700
+ unset($importData[$attribute]);
701
+
702
+ }
703
+
704
+ }
705
+
706
+ // map default attributes
707
+ $productData = array();
708
+ foreach($this->_getDefaultAttributesList() as $backendType => $attributesList) {
709
+ if ($backendType != 'spec') {
710
+ foreach($attributesList as $attribute) {
711
+ if (isset($importData[$attribute])) {
712
+ $productData[$backendType][$attribute] = $importData[$attribute];
713
+ unset($importData[$attribute]);
714
+ }
715
+ }
716
+ } else {
717
+ foreach($attributesList as $attributeCode => $attributeSpecs) {
718
+ if (isset($importData[$attributeCode])) {
719
+ $attributeSpecs['value'] = $importData[$attributeCode];
720
+ $productData[$backendType][$attributeCode] = $attributeSpecs;
721
+ unset($importData[$attributeCode]);
722
+ }
723
+ }
724
+ }
725
+ }
726
+
727
+ // map custom attributes
728
+ if (!empty($importData)) {
729
+ foreach ($importData as $attributeCode => $value) {
730
+ $backendTypeFetch = $this->_connRes->fetchRow("SELECT backend_type FROM `" . $this->_tablePrefix . "eav_attribute` WHERE `attribute_code` = :code", array(':code' => $attributeCode));
731
+ $backendType = $backendTypeFetch['backend_type'];
732
+ if ($backendType != 'static' && !empty($backendType)) {
733
+ $productData[$backendType][$attributeCode] = $value;
734
+ unset($importData[$attributeCode]);
735
+ }
736
+ }
737
+ }
738
+
739
+ $failedAttributes = array();
740
+ if (count($importData) > 0) {
741
+ $failedAttributes = array_keys($importData);
742
+ }
743
+
744
+ return array(
745
+ 'iceimportAttributes' => $iceAttributes,
746
+ 'productData' => $productData,
747
+ 'failedAttributes' => $failedAttributes
748
+ );
749
+
750
+ }
751
+
752
+ protected function _getDefaultAttributesList () {
753
+ return array(
754
+ 'varchar' => array(
755
+ 'gift_message_available',
756
+ 'custom_design',
757
+ 'msrp_display_actual_price_type',
758
+ 'msrp_enabled',
759
+ 'options_container',
760
+ 'page_layout',
761
+ 'mpn',
762
+ 'brand_name',
763
+ 'name',
764
+ 'url_key',
765
+ 'meta_description',
766
+ 'meta_title'
767
+ ),
768
+ 'int' => array(
769
+ 'enable_googlecheckout',
770
+ 'is_recurring',
771
+ 'links_purchased_separately',
772
+ 'links_exist',
773
+ 'status',
774
+ 'visibility',
775
+ 'tax_class_id',
776
+ 'color',
777
+ 'price_view',
778
+ 'manufacturer'
779
+ ),
780
+ 'text' => array(
781
+ 'recurring_profile',
782
+ 'description',
783
+ 'custom_layout_update',
784
+ 'meta_keyword',
785
+ 'short_description'
786
+ ),
787
+ 'decimal' => array(
788
+ 'cost',
789
+ 'group_price',
790
+ 'weight',
791
+ 'special_price',
792
+ 'msrp'
793
+ ),
794
+ 'datetime' => array(
795
+ 'custom_design_from',
796
+ 'custom_design_to',
797
+ 'news_from_date',
798
+ 'news_to_date',
799
+ 'special_from_date',
800
+ 'special_to_date'
801
+ ),
802
+ 'spec' => array(
803
+ 'is_qty_decimal' => array(
804
+ 'prod_id_field' => 'product_id',
805
+ 'table' => 'cataloginventory_stock_item',
806
+ 'field' => 'is_qty_decimal'
807
+ ),
808
+ 'use_config_min_qty' => array(
809
+ 'prod_id_field' => 'product_id',
810
+ 'table' => 'cataloginventory_stock_item',
811
+ 'field' => 'use_config_min_qty'
812
+ ),
813
+ 'use_config_min_sale_qty' => array(
814
+ 'prod_id_field' => 'product_id',
815
+ 'table' => 'cataloginventory_stock_item',
816
+ 'field' => 'use_config_min_sale_qty'
817
+ ),
818
+ 'use_config_max_sale_qty' => array(
819
+ 'prod_id_field' => 'product_id',
820
+ 'table' => 'cataloginventory_stock_item',
821
+ 'field' => 'use_config_max_sale_qty'
822
+ ),
823
+ 'use_config_manage_stock' => array(
824
+ 'prod_id_field' => 'product_id',
825
+ 'table' => 'cataloginventory_stock_item',
826
+ 'field' => 'use_config_manage_stock'
827
+ ),
828
+ 'is_decimal_divided' => array(
829
+ 'prod_id_field' => 'product_id',
830
+ 'table' => 'cataloginventory_stock_item',
831
+ 'field' => 'is_decimal_divided'
832
+ ),
833
+ 'use_config_backorders' => array(
834
+ 'prod_id_field' => 'product_id',
835
+ 'table' => 'cataloginventory_stock_item',
836
+ 'field' => 'use_config_backorders'
837
+ ),
838
+ 'use_config_notify_stock_qty' => array(
839
+ 'prod_id_field' => 'product_id',
840
+ 'table' => 'cataloginventory_stock_item',
841
+ 'field' => 'use_config_notify_stock_qty'
842
+ ),
843
+ 'max_sale_qty' => array(
844
+ 'prod_id_field' => 'product_id',
845
+ 'table' => 'cataloginventory_stock_item',
846
+ 'field' => 'max_sale_qty'
847
+ ),
848
+ 'min_sale_qty' => array(
849
+ 'prod_id_field' => 'product_id',
850
+ 'table' => 'cataloginventory_stock_item',
851
+ 'field' => 'min_sale_qty'
852
+ ),
853
+ 'notify_stock_qty' => array(
854
+ 'prod_id_field' => 'product_id',
855
+ 'table' => 'cataloginventory_stock_item',
856
+ 'field' => 'notify_stock_qty'
857
+ ),
858
+ 'backorders' => array(
859
+ 'prod_id_field' => 'product_id',
860
+ 'table' => 'cataloginventory_stock_item',
861
+ 'field' => 'backorders'
862
+ ),
863
+ 'created_at' => array(
864
+ 'prod_id_field' => 'entity_id',
865
+ 'table' => 'catalog_product_entity',
866
+ 'field' => 'created_at'
867
+ ),
868
+ 'min_qty' => array(
869
+ 'prod_id_field' => 'product_id',
870
+ 'table' => 'cataloginventory_stock_item',
871
+ 'field' => 'min_qty'
872
+ ),
873
+ 'updated_at' => array(
874
+ 'prod_id_field' => 'entity_id',
875
+ 'table' => 'catalog_product_entity',
876
+ 'field' => 'updated_at'
877
+ )
878
+ )
879
+ );
880
+ }
881
+
882
+ // Count child categories products and set them inactive if they have no products
883
+ public function CountChildProd($child_cat) {
884
+ foreach ($child_cat as $cat) {
885
+ $query = "SELECT `entity_id` FROM `" . $this->_tablePrefix . "catalog_category_entity` WHERE parent_id = :cat_id";
886
+ $child_cat = $this->_connRes->fetchAll($query,array(
887
+ ':cat_id'=> $cat['entity_id']
888
+ ));
889
+
890
+ $query = "SELECT COUNT(*) FROM `" . $this->_tablePrefix . "catalog_category_product` WHERE category_id = :cat_id ";
891
+ $cat_products = $this->_connRes->fetchRow($query,array(
892
+ ':cat_id'=> $cat['entity_id']
893
+ ));
894
+
895
+ if ($cat_products['COUNT(*)'] == 0 && empty($child_cat)) {
896
+ $this->_connRes->query("UPDATE `" . $this->_tablePrefix . "catalog_category_entity_int`
897
+ SET `value` = 0 WHERE `attribute_id` = @category_active_id AND entity_id = :cat_id",array(
898
+ ':cat_id'=> $cat['entity_id']
899
+ ));
900
+ } else if (!empty($child_cat)) {
901
+ $this->CountChildProd($child_cat);
902
+ }
903
+ }
904
+ }
905
+
906
+ }
907
+
app/code/community/Capacitywebsolutions/Importproduct/etc/config.xml CHANGED
@@ -2,7 +2,7 @@
2
  <config>
3
  <modules>
4
  <Capacitywebsolutions_Importproduct>
5
- <version>1.0</version>
6
  </Capacitywebsolutions_Importproduct>
7
  </modules>
8
  <global>
2
  <config>
3
  <modules>
4
  <Capacitywebsolutions_Importproduct>
5
+ <version>1.4.0</version>
6
  </Capacitywebsolutions_Importproduct>
7
  </modules>
8
  <global>
package.xml CHANGED
@@ -1,7 +1,7 @@
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>
@@ -14,10 +14,10 @@
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>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>ice_import</name>
4
+ <version>1.4.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>
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&#xD; -Added imported products to all categories listed in import file for product including Default webshop's category </notes>
18
  <authors><author><name>Iceshop</name><user>Iceshop</user><email>support@iceshop.nl</email></author></authors>
19
+ <date>2013-08-23</date>
20
+ <time>12:07: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>