-
Notifications
You must be signed in to change notification settings - Fork 7
/
product-add-conf.php
executable file
·158 lines (145 loc) · 5.99 KB
/
product-add-conf.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/php
<?php
/**
* Create a configurable product using 'Women Clothing' attribute set, that uses 'item_color' and 'item_size' configurable attributes.
* The Attribute Set code is predefined.
* The Attr
*/
require_once 'lib/init.php';
require_once 'lib/product_functions.php';
$opts = getopt('', array('sku:', 'name:', 'price:', 'cats:', 'weight:', 'store:', 'set:', 'summary:', 'desc:',
'variants:', 'webs:'));
if (empty($opts) || empty($opts['sku']) || empty($opts['name']) || empty($opts['price']) || empty($opts['variants'])) {
echo "Usage: product-add-conf.php --sku SKU --name NAME --price PRICE --variants COLOR/SIZE:QTY,... [--cats URL_KEY,...] [--summary SUMMARY] [--desc DESCRIPTION] [--weight WEIGHT] [--store STORE_ID] [--set ATTRIBUTE_SET_ID] [--webs CODE,...]\n";
exit(1);
}
echo 'Loading websites...';
$websites = Mage::getModel('core/website')->getCollection()->setLoadDefault(false);
$websiteLookup = array();
foreach ($websites as $website) {
$websiteLookup[$website->getCode()] = $website->getWebsiteId();
}
echo 'x '. join(' ', array_keys($websiteLookup)) ."\n";
echo 'Loading categories...';
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*');
$categoryLookup = array();
$defaultCategoryIds = array();
foreach ($categories as $cat) {
if ($cat->getUrlKey() !== '')
$categoryLookup[$cat->getUrlKey()] = $cat->getId();
if ($cat->getLevel() <= 1 && $cat->getIsActive())
$defaultCategoryIds[] = $cat->getId();
}
echo join(' ', array_keys($categoryLookup)) .". defaults: ". join(' ', $defaultCategoryIds) . "\n";
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;
}
echo ' '. count($sets) . " found.\n";
echo "Loading user defined attributes...\n";
$udAttrs = Mage::getResourceModel('catalog/product_attribute_collection');
$udAttrs->addFieldToFilter('main_table.is_user_defined', 1);
$udAttrLookup = array();
$udAttrCodes = array();
$optionLookup = array();
foreach ($udAttrs as $attr) {
$udAttrCodes[] = $attr->getAttributeCode();
$udAttrLookup[$attr->getAttributeCode()] = $attr;
if ($attr->usesSource()) {
$valueLookup = array();
$source = $attr->getSource();
foreach ($source->getAllOptions() as $optionOrder => $optionValue) {
if (empty($optionOrder) || empty($optionValue))
continue;
$valueLookup[trim($optionValue['label'])] = $optionValue['value'];
//echo "value-label : ".trim($optionValue['label'])." - ";
}
$optionLookup[$attr->getAttributeCode()] = $valueLookup;
echo $attr->getAttributeCode() .': '. join(' ', array_keys($valueLookup)) ."\n";
}
}
$item_color_attrId = $udAttrLookup['item_color']->getId();
$item_size_attrId = $udAttrLookup['item_size']->getId();
echo "Attribute IDs: item_color=$item_color_attrId item_size=$item_size_attrId\n";
$sku = $opts['sku'];
$name = $opts['name'];
$price = $opts['price'];
$variants = !empty($opts['variants']) ? $opts['variants'] : '';
$storeId = !empty($opts['store']) ? $opts['store'] : DEFAULT_STORE_ID;
$set = !empty($opts['set']) ? $opts['set'] : 'Default';
$summary = !empty($opts['summary']) ? $opts['summary'] : $name;
$description = !empty($opts['desc']) ? $opts['desc'] : $summary;
$cats = !empty($opts['cats']) ? $opts['cats'] : '';
$weight = !empty($opts['weight']) ? $opts['weight'] : 1.0;
$webs = !empty($opts['webs']) ? $opts['webs'] : join(',', array_keys($websiteLookup)); // if not specified, select all websites
echo "Create configurable product $sku name: $name set: $set price: $price variants: $variants cats: $cats webs: $webs\n";
$webCodes = split(',', $webs);
$websiteIds = array();
foreach ($webCodes as $webCode) {
if (!isset($websiteLookup[$webCode]))
throw new Exception("Cannot find website '$webCode'");
$websiteIds[] = $websiteLookup[$webCode];
}
echo 'Website IDs: '. join(' ', $websiteIds) ."\n";
$catKeys = split(',', $cats);
$categoryIds = $defaultCategoryIds;
foreach ($catKeys as $catKey) {
if (!isset($categoryLookup[$catKey]))
throw new Exception("Cannot find category '$catKey'");
$categoryId = $categoryLookup[$catKey];
if (!in_array($categoryId, $categoryIds))
$categoryIds[] = $categoryId;
}
echo 'Category IDs: '. join(' ', $categoryIds) ."\n";
if (!isset($sets[$set]))
throw new Exception("Cannot find attribute set '$set'");
$setId = $sets[$set]->getId();
echo "Attribute set ID: $setId\n";
$variantCodes = split(',', $variants);
// Generate the child products
$variantsData = array();
foreach ($variantCodes as $variantCode) {
if (!preg_match('/^(.+)\\/(.+):(.+)$/', $variantCode, $matches))
throw new Exception("Invalid variant code: $variantCode");
list($dummy, $color, $size, $qty) = $matches;
if (!isset($optionLookup['item_color'][$color])) {
throw new Exception("Cannot find option value for item_color '$color'");
}
$colorId = $optionLookup['item_color'][$color];
if (!isset($optionLookup['item_size'][$size])) {
throw new Exception("Cannot find option value for item_size '$size'");
}
$sizeId = $optionLookup['item_size'][$size];
$variantSku = $sku .'-'. $color .'-'. $size;
$variantName = $name .'-'. $color .'-'. $size;
echo "Variant $variantSku $variantName: qty=$qty item_color=$colorId:$color item_size=$sizeId:$size\n";
$variantsData[] = array(
'sku' => $variantSku,
'name' => $variantName,
'qty' => $qty,
'item_color' => $colorId,
'item_size' => $sizeId
);
}
// Create the childs products and configurable product
createConfigurableProduct(
array(
'item_color_attrId' => $item_color_attrId,
'item_size_attrId' => $item_size_attrId),
array(
'storeId' => $storeId,
'setId' => $setId,
'sku' => $sku,
'name' => $name,
'summary' => $summary,
'description' => $description,
'weight' => $weight,
'price' => $price,
'categoryIds' => $categoryIds,
'websiteIds' => $websiteIds),
$variantsData);