Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 0946677

Browse files
author
Tyler Mills
committed
Initial commit
0 parents  commit 0946677

File tree

5 files changed

+327
-0
lines changed

5 files changed

+327
-0
lines changed

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "etre2/magento-improved-import",
3+
"description": "BETA",
4+
"type": "magento-module",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Tyler Mills",
9+
"email": "tylerssn@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.6.0",
14+
"magento-hackathon/magento-composer-installer": "*"
15+
},
16+
"extra":{
17+
"magento-root-dir":"./",
18+
"map":[
19+
["src/app/etc/modules/Etre_ImprovedImport.xml", "app/etc/modules/Etre_ImprovedImport.xml"],
20+
["src/app/code/local/Etre/ImprovedImport", "app/code/local/Etre/ImprovedImport"]
21+
]
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: tyler
5+
* Date: 1/31/17
6+
* Time: 2:17 PM
7+
*/
8+
class Etre_ImprovedImport_Helper_Data extends Mage_Core_Helper_Abstract {
9+
10+
}
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: tyler
6+
* Date: 1/31/17
7+
* Time: 2:18 PM
8+
*/
9+
class Etre_ImprovedImport_Model_Rewrite_Catalog_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
10+
{
11+
12+
13+
/**
14+
* Save product (import)
15+
*
16+
* @param array $importData
17+
* @throws Mage_Core_Exception
18+
* @return bool
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')->__('Skipping import row, required field "%s" is not defined.', 'store');
30+
Mage::throwException($message);
31+
}
32+
} else {
33+
$store = $this->getStoreByCode($importData['store']);
34+
}
35+
36+
if ($store === false) {
37+
$message = Mage::helper('catalog')->__('Skipping import row, store "%s" field does not exist.', $importData['store']);
38+
Mage::throwException($message);
39+
}
40+
41+
if (empty($importData['sku'])) {
42+
$message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'sku');
43+
Mage::throwException($message);
44+
}
45+
$product->setStoreId($store->getId());
46+
$productId = $product->getIdBySku($importData['sku']);
47+
48+
if ($productId) {
49+
$product->load($productId);
50+
} else {
51+
$productTypes = $this->getProductTypes();
52+
$productAttributeSets = $this->getProductAttributeSets();
53+
54+
/**
55+
* Check product define type
56+
*/
57+
if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) {
58+
$value = isset($importData['type']) ? $importData['type'] : '';
59+
$message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s".', $value, 'type');
60+
$helperData = $message . ' ' . Mage::helper('etre_improvedimport')->__('Tried loading product by SKU but was unable. Magento then checked for product "type" column to create a new product but it is not defined or is invalid. Data: %s.', json_encode($importData));
61+
Mage::throwException($helperData);
62+
}
63+
$product->setTypeId($productTypes[strtolower($importData['type'])]);
64+
/**
65+
* Check product define attribute set
66+
*/
67+
if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) {
68+
$value = isset($importData['attribute_set']) ? $importData['attribute_set'] : '';
69+
$message = Mage::helper('catalog')->__('Skip import row, the value "%s" is invalid for field "%s"', $value, 'attribute_set');
70+
Mage::throwException($message);
71+
}
72+
$product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]);
73+
74+
foreach ($this->_requiredFields as $field) {
75+
$attribute = $this->getAttribute($field);
76+
if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) {
77+
$message = Mage::helper('catalog')->__('Skipping import row, required field "%s" for new products is not defined.', $field);
78+
Mage::throwException($message);
79+
}
80+
}
81+
}
82+
83+
// process row with media data only
84+
if (isset($importData['_media_image']) && strlen($importData['_media_image'])) {
85+
$this->saveImageDataRow($product, $importData);
86+
return true;
87+
}
88+
89+
$this->setProductTypeInstance($product);
90+
91+
if (isset($importData['category_ids'])) {
92+
$product->setCategoryIds($importData['category_ids']);
93+
}
94+
95+
foreach ($this->_ignoreFields as $field) {
96+
if (isset($importData[$field])) {
97+
unset($importData[$field]);
98+
}
99+
}
100+
101+
if ($store->getId() != 0) {
102+
$websiteIds = $product->getWebsiteIds();
103+
if (!is_array($websiteIds)) {
104+
$websiteIds = array();
105+
}
106+
if (!in_array($store->getWebsiteId(), $websiteIds)) {
107+
$websiteIds[] = $store->getWebsiteId();
108+
}
109+
$product->setWebsiteIds($websiteIds);
110+
}
111+
112+
if (isset($importData['websites'])) {
113+
$websiteIds = $product->getWebsiteIds();
114+
if (!is_array($websiteIds) || !$store->getId()) {
115+
$websiteIds = array();
116+
}
117+
$websiteCodes = explode(',', $importData['websites']);
118+
foreach ($websiteCodes as $websiteCode) {
119+
try {
120+
$website = Mage::app()->getWebsite(trim($websiteCode));
121+
if (!in_array($website->getId(), $websiteIds)) {
122+
$websiteIds[] = $website->getId();
123+
}
124+
} catch (Exception $e) {
125+
}
126+
}
127+
$product->setWebsiteIds($websiteIds);
128+
unset($websiteIds);
129+
}
130+
131+
foreach ($importData as $field => $value) {
132+
if (in_array($field, $this->_inventoryFields)) {
133+
continue;
134+
}
135+
if (is_null($value)) {
136+
continue;
137+
}
138+
139+
$attribute = $this->getAttribute($field);
140+
if (!$attribute) {
141+
continue;
142+
}
143+
144+
$isArray = false;
145+
$setValue = $value;
146+
147+
if ($attribute->getFrontendInput() == 'multiselect') {
148+
$value = explode(self::MULTI_DELIMITER, $value);
149+
$isArray = true;
150+
$setValue = array();
151+
}
152+
153+
if ($value && $attribute->getBackendType() == 'decimal') {
154+
$setValue = $this->getNumber($value);
155+
}
156+
157+
if ($attribute->usesSource()) {
158+
$options = $attribute->getSource()->getAllOptions(false);
159+
160+
if ($isArray) {
161+
foreach ($options as $item) {
162+
if (in_array($item['label'], $value)) {
163+
$setValue[] = $item['value'];
164+
}
165+
}
166+
} else {
167+
$setValue = false;
168+
foreach ($options as $item) {
169+
if (is_array($item['value'])) {
170+
foreach ($item['value'] as $subValue) {
171+
if (isset($subValue['value']) && $subValue['value'] == $value) {
172+
$setValue = $value;
173+
}
174+
}
175+
} else if ($item['label'] == $value) {
176+
$setValue = $item['value'];
177+
}
178+
}
179+
}
180+
}
181+
182+
$product->setData($field, $setValue);
183+
}
184+
185+
if (!$product->getVisibility()) {
186+
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
187+
}
188+
189+
$stockData = array();
190+
$inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()])
191+
? $this->_inventoryFieldsProductTypes[$product->getTypeId()]
192+
: array();
193+
foreach ($inventoryFields as $field) {
194+
if (isset($importData[$field])) {
195+
if (in_array($field, $this->_toNumber)) {
196+
$stockData[$field] = $this->getNumber($importData[$field]);
197+
} else {
198+
$stockData[$field] = $importData[$field];
199+
}
200+
}
201+
}
202+
$product->setStockData($stockData);
203+
204+
$arrayToMassAdd = array();
205+
206+
foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
207+
if (isset($importData[$mediaAttributeCode])) {
208+
$file = trim($importData[$mediaAttributeCode]);
209+
if (!empty($file) && !$this->_galleryBackendModel->getImage($product, $file)) {
210+
$arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
211+
}
212+
}
213+
}
214+
215+
$addedFilesCorrespondence = $this->_galleryBackendModel->addImagesWithDifferentMediaAttributes(
216+
$product,
217+
$arrayToMassAdd, Mage::getBaseDir('media') . DS . 'import',
218+
false,
219+
false
220+
);
221+
222+
foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
223+
$addedFile = '';
224+
if (isset($importData[$mediaAttributeCode . '_label'])) {
225+
$fileLabel = trim($importData[$mediaAttributeCode . '_label']);
226+
if (isset($importData[$mediaAttributeCode])) {
227+
$keyInAddedFile = array_search($importData[$mediaAttributeCode],
228+
$addedFilesCorrespondence['alreadyAddedFiles']);
229+
if ($keyInAddedFile !== false) {
230+
$addedFile = $addedFilesCorrespondence['alreadyAddedFilesNames'][$keyInAddedFile];
231+
}
232+
}
233+
234+
if (!$addedFile) {
235+
$addedFile = $product->getData($mediaAttributeCode);
236+
}
237+
if ($fileLabel && $addedFile) {
238+
$this->_galleryBackendModel->updateImage($product, $addedFile, array('label' => $fileLabel));
239+
}
240+
}
241+
}
242+
243+
$product->setIsMassupdate(true);
244+
$product->setExcludeUrlRewrite(true);
245+
246+
$product->save();
247+
248+
// Store affected products ids
249+
$this->_addAffectedEntityIds($product->getId());
250+
251+
return true;
252+
}
253+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<modules>
4+
<Etre_ImprovedImport>
5+
<version>0.1.0</version>
6+
</Etre_ImprovedImport>
7+
</modules>
8+
<global>
9+
<models>
10+
<etre_improvedimport>
11+
<class>Etre_ImprovedImport_Model</class>
12+
<resourceModel>etre_improvedimport_resource</resourceModel>
13+
</etre_improvedimport>
14+
<etre_improvedimport_resource>
15+
<class>Etre_ImprovedImport_Model_Resource</class>
16+
</etre_improvedimport_resource>
17+
<catalog>
18+
<rewrite>
19+
<convert_adapter_product>Etre_ImprovedImport_Model_Rewrite_Catalog_Convert_Adapter_Product</convert_adapter_product>
20+
</rewrite>
21+
</catalog>
22+
</models>
23+
<helpers>
24+
<etre_improvedimport>
25+
<class>Etre_ImprovedImport_Helper</class>
26+
</etre_improvedimport>
27+
</helpers>
28+
</global>
29+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<modules>
4+
<Etre_ImprovedImport>
5+
<active>true</active>
6+
<codePool>local</codePool>
7+
<depends>
8+
<Mage_Catalog/>
9+
10+
</depends> </Etre_ImprovedImport>
11+
</modules>
12+
</config>

0 commit comments

Comments
 (0)