-
Notifications
You must be signed in to change notification settings - Fork 20
/
import_arrtibute_set.php
79 lines (67 loc) · 2.79 KB
/
import_arrtibute_set.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
class ArrtibuteSetImporter
{
private $_storeIds = array();
private $_groups = array();
private $_attributeSetModel;
private $_entityTypeId;
public function __construct()
{
$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val) {
$this->_storeIds[] = Mage::app()->getStore($_eachStoreId)->getId();
}
$this->_attributeSetModel = Mage::getModel('eav/entity_setup', 'core_write');
$this->_entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
}
public function import($fileName)
{
echo "Reading $fileName.\n";
$file = fopen($fileName, "r");
while (!feof($file)) {
$csv[] = fgetcsv($file, 0, ',');
}
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
$currentSet = null;
foreach ($csv as $row) {
if (trim($row['ID']) != '') {
$currentSet = $row['NAME'];
$this->_groups[$currentSet] = array('sku'=>explode(';', $row['PRODUCT SKU']));
echo "Creating Attribute Set [$currentSet].\n";
$this->_attributeSetModel->addAttributeSet($this->_entityTypeId, $currentSet);
} else {
$this->_groups[$currentSet]['groups'][] = array(
'name'=>$row['NAME'],
'attributes'=>explode(';', $row['DEFAULT GROUP'])
);
}
}
echo "MEMORY USED : ".convert(memory_get_usage(true)) . "\n";
$this->processAtributeGroup($this->_groups);
fclose($file);
unset($csv, $keys, $file);
echo "MEMORY USED : ".convert(memory_get_usage(true)) . "\n";
}
protected function processAtributeGroup($sets)
{
foreach ($sets as $setId => $setInfo) {
echo "Adding Group to Attribute Set [$setId].\n";
foreach ($setInfo['groups'] as $_group) {
if(!empty($_group['name'])) {
$this->_attributeSetModel->addAttributeGroup($this->_entityTypeId, $setId, $_group['name']);
echo " Adding Attribute to Group [".$_group['name']."].\n";
foreach ($_group['attributes'] as $_attribute) {
$attribute = explode('/', $_attribute);
if(!empty($attribute[0])) {
echo " ->Adding [".$attribute[0]."]\n";
$this->_attributeSetModel->addAttributeToSet($this->_entityTypeId, $setId, $_group['name'], $attribute[0]);
}
}
}
}
}
}
}