-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexport_attribute_set.php
95 lines (77 loc) · 3.72 KB
/
export_attribute_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
class ArrtibuteSetExporter
{
private $_storeIds = array();
private $_entityTypeId;
public function __construct()
{
$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId => $val) {
$this->_storeIds[] = Mage::app()->getStore($_eachStoreId)->getId();
}
$this->_entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
}
public function export($attributeSetNames, $filename, $pullProducts = false)
{
$exportProducts = array();
$exportAttributes = array();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter($this->_entityTypeId)
->addFieldToFilter('attribute_set_name', array('in', $attributeSetNames));
$attibute_set = array();
$i = 0;
foreach ($collection as $attributeSet) {
$defaultGroupId = $attributeSet->getDefaultGroupId();
$defaultGroup = Mage::getModel('eav/entity_attribute_group')->load($attributeSet->getDefaultGroupId());
$attibute_set[$i]['ID'] = $attributeSet->getId();
$attibute_set[$i]['NAME'] = $attributeSet->getAttributeSetName();
$attibute_set[$i]['Group ID'] = $defaultGroupId;
$attibute_set[$i]['DEFAULT GROUP'] = $defaultGroup->getAttributeGroupName();
if ($pullProducts) {
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('sku')
->addFieldToFilter('attribute_set_id', $attributeSet->getId());
$productsSku = array();
foreach ($products as $p) {
$productsSku[] = $p->getSku();
$exportProducts[] = $p->getSku();
}
unset($productsSku, $products);
$attibute_set[$i]['PRODUCT SKU'] = join(';', $productsSku);
} else {
$attibute_set[$i]['PRODUCT SKU'] = ' ';
}
/** @var $groupCollection Mage_Eav_Model_Resource_Entity_Attribute_Group_Collection */
$groupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
->setAttributeSetFilter($attributeSet->getId())
->addOrder('sort_order', 'ASC')
->load();
foreach ($groupCollection as $group) {
$i++;
/* @var $group Mage_Eav_Model_Entity_Attribute_Group */
$attrs = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeGroupFilter($group->getId())
->addVisibleFilter()
->checkConfigurableProducts();
$attrCodes = array();
foreach ($attrs as $attr) {
$attrCodes[] = $attr->getAttributeCode() .'/'. $attr->getSortOrder();
$exportAttributes[] = '"'.$attr->getAttributeCode().'"';
}
$attibute_set[$i]['ID'] = ' ';
$attibute_set[$i]['NAME'] = $group->getAttributeGroupName();
$attibute_set[$i]['Group ID'] = ' ';
$attibute_set[$i]['DEFAULT GROUP'] = join(';', $attrCodes);
$attibute_set[$i]['PRODUCT SKU'] = ' ';
}
unset($attrCodes, $attrs, $groupCollection);
$i++;
}
unset($collection);
echo "preparing csv.... \n";
writeCsv($attibute_set, $filename, ',');
unset($attibute_set);
return [ $exportAttributes, $exportProducts ];
}
}