-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathattrset-import.php
executable file
·67 lines (59 loc) · 2 KB
/
attrset-import.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
#!/usr/bin/php
<?php
require_once 'lib/init.php';
require_once 'lib/attributeset_functions.php';
if (count($argv) < 2) {
echo "Import attribute sets\n";
echo "Usage: attrset-import.php INPUT_XML\n";
exit(1);
}
echo "Loading attribute sets...";
$entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter($entityType->getId());
$sets = array();
foreach ($collection as $attributeSet) {
$sets[$attributeSet->getAttributeSetName()] = $attributeSet->getId();
}
echo ' '. count($sets) . " found.\n";
echo "Loading user defined attributes...";
$udAttrs = Mage::getResourceModel('catalog/product_attribute_collection');
$udAttrs->addFieldToFilter('main_table.is_user_defined', 1);
$udAttrLookup = array();
$udAttrCodes = array();
foreach ($udAttrs as $attr) {
$udAttrCodes[] = $attr->getAttributeCode();
$udAttrLookup[$attr->getAttributeCode()] = $attr;
}
echo ' '. join(' ', $udAttrCodes) ."\n";
// Load File XML
$xmlFilename = $argv[1];
echo "Loading $xmlFilename...";
$attrsetsXml = simplexml_load_file($xmlFilename);
echo " Loaded.\n";
foreach ($attrsetsXml as $setEl) {
$name = (string) $setEl->name;
$base = (string) $setEl->base;
$attributesStr = (string) $setEl->attributesStr;
// check if set with requested $skeletonSetId exists
if (!isset($sets[$base])) {
throw new Exception("Cannot find base attribute set '$base'");
}
$baseId = $sets[$base];
$attributeIds = array();
$attrCodes = isset($attributesStr) ? explode(',', $attributesStr) : array();
echo "Lookup attributes...";
foreach ($attrCodes as $attrCode) {
echo " $attrCode";
if (!isset($udAttrLookup[$attrCode])) {
echo "Attribute $attrCode not found, skipping!\n";
continue;
}
$attributeId = $udAttrLookup[$attrCode]->getId();
echo "=$attributeId";
$attributeIds[] = $attributeId;
}
echo " Lookup complete.\n";
$attributeSetId = createAttributeSet($baseId, $name, $attributeIds);
$sets[$name] = $attributeSetId;
}