CapacityWebSolutions - Version 1.0

Version Notes

Please take back before install this extension.

Download this release

Release Info

Developer Magento Core Team
Extension CapacityWebSolutions
Version 1.0
Comparing to
See all releases


Version 1.0

app/code/community/CapacityWebSolutions/ImportProduct/Model/Convert/Adapter/Product.php ADDED
@@ -0,0 +1,412 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ class CapacityWebSolutions_ImportProduct_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
9
+ {
10
+ /**
11
+ * Save product (import)
12
+ *
13
+ * @param array $importData
14
+ * @throws Mage_Core_Exception
15
+ * @return bool
16
+ */
17
+ protected $custom_options = array();
18
+
19
+
20
+ public function saveRow(array $importData)
21
+ {
22
+ $product = $this->getProductModel()
23
+ ->reset();
24
+
25
+ if (empty($importData['store'])) {
26
+ if (!is_null($this->getBatchParams('store'))) {
27
+ $store = $this->getStoreById($this->getBatchParams('store'));
28
+ } else {
29
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
30
+ Mage::throwException($message);
31
+ }
32
+ }
33
+ else {
34
+ $store = $this->getStoreByCode($importData['store']);
35
+ }
36
+
37
+ if ($store === false) {
38
+ $message = Mage::helper('catalog')->__('Skip import row, store "%s" field not exists', $importData['store']);
39
+ Mage::throwException($message);
40
+ }
41
+
42
+ if (empty($importData['sku'])) {
43
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
44
+ Mage::throwException($message);
45
+ }
46
+ $product->setStoreId($store->getId());
47
+ $productId = $product->getIdBySku($importData['sku']);
48
+
49
+ if ($productId) {
50
+ $product->load($productId);
51
+ }
52
+ else {
53
+ $productTypes = $this->getProductTypes();
54
+ $productAttributeSets = $this->getProductAttributeSets();
55
+
56
+ /**
57
+ * Check product define type
58
+ */
59
+ if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) {
60
+ $value = isset($importData['type']) ? $importData['type'] : '';
61
+ $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'type');
62
+ Mage::throwException($message);
63
+ }
64
+ $product->setTypeId($productTypes[strtolower($importData['type'])]);
65
+ /**
66
+ * Check product define attribute set
67
+ */
68
+ if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) {
69
+ /*$value = isset($importData['attribute_set']) ? $importData['attribute_set'] : '';
70
+ $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'attribute_set');
71
+ Mage::throwException($message);*/
72
+ }
73
+ $product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]);
74
+
75
+ foreach ($this->_requiredFields as $field) {
76
+ $attribute = $this->getAttribute($field);
77
+ if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) {
78
+ $message = Mage::helper('catalog')->__('Skip import row, required field "%s" for new products not defined', $field);
79
+ Mage::throwException($message);
80
+ }
81
+ }
82
+ }
83
+
84
+ $this->setProductTypeInstance($product);
85
+
86
+ if (isset($importData['category_ids'])) {
87
+ $product->setCategoryIds($importData['category_ids']);
88
+ }
89
+ /* if category name is in csv file */
90
+ if (isset($importData['categories'])) {
91
+
92
+ $categoryIds = $this->_addCategories($importData['categories'], $store);
93
+ if ($categoryIds) {
94
+ $product->setCategoryIds($categoryIds);
95
+ }
96
+ }
97
+ foreach ($this->_ignoreFields as $field) {
98
+ if (isset($importData[$field])) {
99
+ unset($importData[$field]);
100
+ }
101
+ }
102
+
103
+ if ($store->getId() != 0) {
104
+ $websiteIds = $product->getWebsiteIds();
105
+ if (!is_array($websiteIds)) {
106
+ $websiteIds = array();
107
+ }
108
+ if (!in_array($store->getWebsiteId(), $websiteIds)) {
109
+ $websiteIds[] = $store->getWebsiteId();
110
+ }
111
+ $product->setWebsiteIds($websiteIds);
112
+ }
113
+
114
+ if (isset($importData['websites'])) {
115
+ $websiteIds = $product->getWebsiteIds();
116
+ if (!is_array($websiteIds)) {
117
+ $websiteIds = array();
118
+ }
119
+ $websiteCodes = explode(',', $importData['websites']);
120
+ foreach ($websiteCodes as $websiteCode) {
121
+ try {
122
+ $website = Mage::app()->getWebsite(trim($websiteCode));
123
+ if (!in_array($website->getId(), $websiteIds)) {
124
+ $websiteIds[] = $website->getId();
125
+ }
126
+ }
127
+ catch (Exception $e) {}
128
+ }
129
+ $product->setWebsiteIds($websiteIds);
130
+ unset($websiteIds);
131
+ }
132
+
133
+ foreach ($importData as $field => $value) {
134
+ if (in_array($field, $this->_inventoryFields)) {
135
+ continue;
136
+ }
137
+ if (in_array($field, $this->_imageFields)) {
138
+ continue;
139
+ }
140
+ $attribute = $this->getAttribute($field);
141
+ if (!$attribute) {
142
+
143
+ if(strpos($field,':')!==FALSE && strlen($value)) {
144
+ $values=explode('|',$value);
145
+ if(count($values)>0) {
146
+ @list($title,$type,$is_required,$sort_order) = explode(':',$field);
147
+ $title = ucfirst(str_replace('_',' ',$title));
148
+ $custom_options[] = array(
149
+ 'is_delete'=>0,
150
+ 'title'=>$title,
151
+ 'previous_group'=>'',
152
+ 'previous_type'=>'',
153
+ 'type'=>$type,
154
+ 'is_require'=>$is_required,
155
+ 'sort_order'=>$sort_order,
156
+ 'values'=>array()
157
+ );
158
+ foreach($values as $v) {
159
+ $parts = explode(':',$v);
160
+ $title = $parts[0];
161
+ if(count($parts)>1) {
162
+ $price_type = $parts[1];
163
+ } else {
164
+ $price_type = 'fixed';
165
+ }
166
+ if(count($parts)>2) {
167
+ $price = $parts[2];
168
+ } else {
169
+ $price =0;
170
+ }
171
+ if(count($parts)>3) {
172
+ $sku = $parts[3];
173
+ } else {
174
+ $sku='';
175
+ }
176
+ if(count($parts)>4) {
177
+ $sort_order = $parts[4];
178
+ } else {
179
+ $sort_order = 0;
180
+ }
181
+ switch($type) {
182
+ case 'file':
183
+ /* TODO */
184
+ break;
185
+
186
+ case 'field':
187
+ case 'area':
188
+ $custom_options[count($custom_options) - 1]['max_characters'] = $sort_order;
189
+ /* NO BREAK */
190
+
191
+ case 'date':
192
+ case 'date_time':
193
+ case 'time':
194
+ $custom_options[count($custom_options) - 1]['price_type'] = $price_type;
195
+ $custom_options[count($custom_options) - 1]['price'] = $price;
196
+ $custom_options[count($custom_options) - 1]['sku'] = $sku;
197
+ break;
198
+
199
+ case 'drop_down':
200
+ case 'radio':
201
+ case 'checkbox':
202
+ case 'multiple':
203
+ default:
204
+ $custom_options[count($custom_options) - 1]['values'][]=array(
205
+ 'is_delete'=>0,
206
+ 'title'=>$title,
207
+ 'option_type_id'=>-1,
208
+ 'price_type'=>$price_type,
209
+ 'price'=>$price,
210
+ 'sku'=>$sku,
211
+ 'sort_order'=>$sort_order,
212
+ );
213
+ break;
214
+ }
215
+ }
216
+ }
217
+ }
218
+
219
+ continue;
220
+ }
221
+
222
+ $isArray = false;
223
+ $setValue = $value;
224
+
225
+ if ($attribute->getFrontendInput() == 'multiselect') {
226
+ $value = explode(self::MULTI_DELIMITER, $value);
227
+ $isArray = true;
228
+ $setValue = array();
229
+ }
230
+
231
+ if ($value && $attribute->getBackendType() == 'decimal') {
232
+ $setValue = $this->getNumber($value);
233
+ }
234
+
235
+
236
+ if ($attribute->usesSource()) {
237
+ $options = $attribute->getSource()->getAllOptions(false);
238
+
239
+ if ($isArray) {
240
+ foreach ($options as $item) {
241
+ if (in_array($item['label'], $value)) {
242
+ $setValue[] = $item['value'];
243
+ }
244
+ }
245
+ }
246
+ else {
247
+ $setValue = null;
248
+ foreach ($options as $item) {
249
+ if ($item['label'] == $value) {
250
+ $setValue = $item['value'];
251
+ }
252
+ }
253
+ }
254
+ }
255
+
256
+ $product->setData($field, $setValue);
257
+ }
258
+
259
+ if (!$product->getVisibility()) {
260
+ $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
261
+ }
262
+
263
+ $stockData = array();
264
+ $inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()])
265
+ ? $this->_inventoryFieldsProductTypes[$product->getTypeId()]
266
+ : array();
267
+ foreach ($inventoryFields as $field) {
268
+ if (isset($importData[$field])) {
269
+ if (in_array($field, $this->_toNumber)) {
270
+ $stockData[$field] = $this->getNumber($importData[$field]);
271
+ }
272
+ else {
273
+ $stockData[$field] = $importData[$field];
274
+ }
275
+ }
276
+ }
277
+ $product->setStockData($stockData);
278
+
279
+ $imageData = array();
280
+ foreach ($this->_imageFields as $field) {
281
+ if (!empty($importData[$field]) && $importData[$field] != 'no_selection') {
282
+ if (!isset($imageData[$importData[$field]])) {
283
+ $imageData[$importData[$field]] = array();
284
+ }
285
+ $imageData[$importData[$field]][] = $field;
286
+ }
287
+ }
288
+
289
+ foreach ($imageData as $file => $fields) {
290
+ try {
291
+ $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
292
+ }
293
+ catch (Exception $e) {}
294
+ }
295
+
296
+ /**
297
+ * Allows you to import multiple images for each product.
298
+ * Simply add a 'gallery' column to the import file, and separate
299
+ * each image with a semi-colon.
300
+ */
301
+ try {
302
+ $galleryData = explode(';',$importData["gallery"]);
303
+ foreach($galleryData as $gallery_img)
304
+ /**
305
+ * @param directory where import image resides
306
+ * @param leave 'null' so that it isn't imported as thumbnail, base, or small
307
+ * @param false = the image is copied, not moved from the import directory to it's new location
308
+ * @param false = not excluded from the front end gallery
309
+ */
310
+ {
311
+ $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false);
312
+ }
313
+ }
314
+ catch (Exception $e) {}
315
+ /* End Modification */
316
+
317
+ $product->setIsMassupdate(true);
318
+ $product->setExcludeUrlRewrite(true);
319
+
320
+ $product->save();
321
+ /* Add the custom options specified in the CSV import file */
322
+
323
+ if(isset($custom_options)){
324
+ if(count($custom_options)) {
325
+ foreach($custom_options as $option) {
326
+ try {
327
+ $opt = Mage::getModel('catalog/product_option');
328
+ $opt->setProduct($product);
329
+ $opt->addOption($option);
330
+ $opt->saveOptions();
331
+ }
332
+ catch (Exception $e) {}
333
+ }
334
+ }
335
+ }
336
+ return true;
337
+ }
338
+
339
+ protected $_categoryCache = array();
340
+ /* Add category and sub category. */
341
+ protected function _addCategories($categories, $store)
342
+ {
343
+
344
+ $rootId = $store->getRootCategoryId();
345
+
346
+ if (!$rootId) {
347
+ /* If stoder not create that mense admin then assign 1 to storeId */
348
+ $storeId = 1;
349
+ $rootId = Mage::app()->getStore($storeId)->getRootCategoryId();
350
+
351
+ }
352
+
353
+ if($categories=="")
354
+ return array();
355
+ $rootPath = '1/'.$rootId;
356
+ if (empty($this->_categoryCache[$store->getId()])) {
357
+ $collection = Mage::getModel('catalog/category')->getCollection()
358
+ ->setStore($store)
359
+ ->addAttributeToSelect('name');
360
+ $collection->getSelect()->where("path like '".$rootPath."/%'");
361
+
362
+ foreach ($collection as $cat) {
363
+ $pathArr = explode('/', $cat->getPath());
364
+ $namePath = '';
365
+ for ($i=2, $l=sizeof($pathArr); $i<$l; $i++) {
366
+ $name = $collection->getItemById($pathArr[$i])->getName();
367
+ $namePath .= (empty($namePath) ? '' : '/').trim($name);
368
+ }
369
+ $cat->setNamePath($namePath);
370
+ }
371
+
372
+ $cache = array();
373
+ foreach ($collection as $cat) {
374
+ $cache[strtolower($cat->getNamePath())] = $cat;
375
+ $cat->unsNamePath();
376
+ }
377
+ $this->_categoryCache[$store->getId()] = $cache;
378
+ }
379
+ $cache =& $this->_categoryCache[$store->getId()];
380
+
381
+ $catIds = array();
382
+ foreach (explode(',', $categories) as $categoryPathStr) {
383
+ $categoryPathStr = preg_replace('#\s*/\s*#', '/', trim($categoryPathStr));
384
+ if (!empty($cache[$categoryPathStr])) {
385
+ $catIds[] = $cache[$categoryPathStr]->getId();
386
+ continue;
387
+ }
388
+ $path = $rootPath;
389
+ $namePath = '';
390
+ foreach (explode('/', $categoryPathStr) as $catName) {
391
+ $namePath .= (empty($namePath) ? '' : '/').strtolower($catName);
392
+ if (empty($cache[$namePath])) {
393
+ $cat = Mage::getModel('catalog/category')
394
+ ->setStoreId($store->getId())
395
+ ->setPath($path)
396
+ ->setName($catName)
397
+ ->setIsActive(1)
398
+ ->save();
399
+ $cache[$namePath] = $cat;
400
+ }
401
+ $catId = $cache[$namePath]->getId();
402
+ $path .= '/'.$catId;
403
+ }
404
+ if ($catId) {
405
+ $catIds[] = $catId;
406
+ }
407
+ }
408
+ return join(',', $catIds);
409
+ }
410
+
411
+
412
+ }
app/code/community/CapacityWebSolutions/ImportProduct/etc/config.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <CapacityWebSolutions_ImportProduct>
5
+ <version>1.0</version>
6
+ </CapacityWebSolutions_ImportProduct>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <catalog>
11
+ <rewrite>
12
+ <!-- Override Mage_Catalog_Model_Convert_Adapter_Product -->
13
+ <convert_adapter_product>CapacityWebSolutions_ImportProduct_Model_Convert_Adapter_Product</convert_adapter_product>
14
+ </rewrite>
15
+ </catalog>
16
+ </models>
17
+ </global>
18
+ </config>
app/etc/modules/CapacityWebSolutions_ImportProduct.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <CapacityWebSolutions_ImportProduct>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </CapacityWebSolutions_ImportProduct>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>CapacityWebSolutions</name>
4
+ <version>1.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>This extension can import product with custom option, category and multiple images</summary>
10
+ <description>This extension can import product with custom option, category and multiple images</description>
11
+ <notes>Please take back before install this extension.</notes>
12
+ <authors><author><name>CapacityWebSolutions</name><user>auto-converted</user><email>magento@capacitywebsolutions.com</email></author></authors>
13
+ <date>2010-08-07</date>
14
+ <time>12:11:36</time>
15
+ <contents><target name="magecommunity"><dir name="CapacityWebSolutions"><dir name="ImportProduct"><dir name="etc"><file name="config.xml" hash="95e08012dbf6643f7af0d5fd48893ecf"/></dir><dir name="Model"><dir name="Convert"><dir name="Adapter"><file name="Product.php" hash="e6f9f19d5cb068a1f434b1a065c2cd28"/></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="CapacityWebSolutions_ImportProduct.xml" hash="c75cf7ac03c3e04b955251c6ff130bbe"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies/>
18
+ </package>