Skip to content
Browse files

Import product with images and user defined attributes... some progress

  • Loading branch information...
1 parent 6f49da4 commit 2b2b89f5bf1b985bbbca58e981cbd9e4a0400034 @ceefour ceefour committed Jul 3, 2012
Showing with 110 additions and 33 deletions.
  1. +6 −0 lib/category_functions.php
  2. +19 −30 lib/product_functions.php
  3. +85 −3 product-import-img2.php
View
6 lib/category_functions.php
@@ -11,6 +11,12 @@
* @return int Category ID.
*/
function createCategory($parentId, $urlkey, $name, $description, $metaTitle) {
+ // Sanity checks
+ if ($urlkey == '')
+ throw new Exception("Please provide urlkey");
+ if ($name == '')
+ throw new Exception("Please provide name");
+
// This must be 0. Otherwise you won't be able to set description, and probably won't work properly.
$storeId = 0;
View
49 lib/product_functions.php
@@ -3,39 +3,25 @@
/**
* Create simple product
*
- * @param array $modelData Model data as map: item_color_attrId, item_size_attrId.
- * Example: <code>array('item_color_attrId' => 123, 'item_size_attrId' => 145)</code>
* @param array $productData Product data as map: storeId, setId, sku, name, summary, description, weight, price, categoryIds, websiteIds,
* Example: <pre>
* array(
- * 'storeId' => 1,
- * 'setId' => 9,
- * 'sku' => 'zibalabel_t01',
- * 'name' => 'Ziba Label T01',
- * 'summary' => 'Tas yang sangat bagus dan lucu',
- * 'description' => 'Cocok untuk dipakai belanja',
- * 'weight' => 5,
- * 'price' => 98000,
- * 'categoryIds' => array(4, 5),
- * 'websiteIds' => array(1, 2)
+ * 'storeId' => 1,
+ * 'setId' => 9,
+ * 'sku' => 'zibalabel_t01',
+ * 'name' => 'Ziba Label T01',
+ * 'summary' => 'Tas yang sangat bagus dan lucu',
+ * 'description' => 'Cocok untuk dipakai belanja',
+ * 'weight' => 5,
+ * 'price' => 98000.0,
+ * 'categoryIds' => array(4, 5),
+ * 'websiteIds' => array(1, 2),
+ * 'qty' => 1
* )</pre>
- * @param array $variantsData Variant data as array. Each element is a map with sku, name, item_qty, item_size, and qty.
- * Example: <pre>
- * array(
- * array('sku' => 'zibalabel_t01-Hitam-XL',
- * 'name' => 'Ziba Label T01-Hitam-XL',
- * 'item_color' => 'Hitam',
- * 'item_size' => 'XL',
- * 'qty' => 5),
- * array('sku' => 'zibalabel_t01-Merah-L',
- * 'name' => 'Ziba Label T01-Merah-L',
- * 'item_color' => 'Merah',
- * 'item_size' => 'L',
- * 'qty' => 5)
- * )</pre>
+ * @param array $additionalData Additional data, for example values for user-defined attributes.
* @return int Product ID.
*/
-function createSimpleProduct($productData) {
+function createSimpleProduct($productData, $additionalData = array()) {
// Read parameters
$storeId = $productData['storeId'];
$setId = $productData['setId'];
@@ -47,12 +33,13 @@ function createSimpleProduct($productData) {
$price = $productData['price'];
$categoryIds = $productData['categoryIds'];
$websiteIds = $productData['websiteIds'];
+ $qty = $productData['qty'];
$product = Mage::getModel('catalog/product');
$product->setStoreId($storeId) // is Product.storeId deprecated? seems weird, bcuz Product can be assigned to multiple Websites now
- ->setAttributeSetId($setId)
- ->setTypeId('simple')
- ->setSku($sku);
+ ->setAttributeSetId($setId)
+ ->setTypeId('simple')
+ ->setSku($sku);
$product->setName($name);
$product->setShortDescription($summary);
$product->setDescription($description);
@@ -63,6 +50,8 @@ function createSimpleProduct($productData) {
$product->setCategoryIds($categoryIds);
$product->setTaxClassId(0); // 0=None 2=Taxable Goods 4=Shipping
$product->setWebsiteIds($websiteIds);
+
+ $product->addData($additionalData);
// set stock
$stockData = array('qty' => $qty, 'is_in_stock' => 1, 'use_config_manage_stock' => 1, 'use_backorders' => 1);
View
88 product-import-img2.php
@@ -139,8 +139,6 @@
}
echo join(' ', array_keys($categoryLookup)) .". defaults: ". join(' ', $defaultCategoryIds) . "\n";
-exit(1);
-
echo "Loading attribute sets...";
$entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
@@ -160,6 +158,8 @@
foreach ($udAttrs as $attr) {
$udAttrCodes[] = $attr->getAttributeCode();
$udAttrLookup[$attr->getAttributeCode()] = $attr;
+
+ // if it's a select attribute, map the options
if ($attr->usesSource()) {
$valueLookup = array();
$source = $attr->getSource();
@@ -227,6 +227,85 @@
echo "Attribute set ID: $setId\n";
if ($product->type == 'simple') {
+ $qty = (string)$product->qty;
+ if ($qty == '') {
+ echo "WARNING: $sku/$name has no qty! Setting to 1";
+ }
+
+ $additionalData = array();
+ // Select attributes:
+ // batik_age, batik_technique, (color=not used), condition,
+ // item_color, item_size, leather, lining, (manufacturer=not used),
+ // material, motif, origin, shoe_size, signature
+ if ($product->batikAge != '')
+ $additionalData['batik_age'] = $optionLookup['batik_age'][(string)$product->batikAge];
+ if ($product->batikTechnique != '')
+ $additionalData['batik_technique'] = $optionLookup['batik_technique'][(string)$product->batikTechnique];
+ if ($product->condition != '')
+ $additionalData['condition'] = $optionLookup['condition'][(string)$product->condition];
+ if ($product->item_color != '')
+ $additionalData['item_color'] = $optionLookup['item_color'][(string)$product->itemColor];
+ if ($product->item_size != '')
+ $additionalData['item_size'] = $optionLookup['item_size'][(string)$product->itemSize];
+ if ($product->leather != '')
+ $additionalData['leather'] = $optionLookup['leather'][(string)$product->leather];
+ if ($product->lining != '')
+ $additionalData['lining'] = $optionLookup['lining'][(string)$product->lining];
+ if ($product->material != '')
+ $additionalData['material'] = $optionLookup['material'][(string)$product->material];
+ if ($product->motif != '')
+ $additionalData['motif'] = $optionLookup['motif'][(string)$product->motif];
+ if ($product->origin != '')
+ $additionalData['origin'] = $optionLookup['origin'][(string)$product->origin];
+ if ($product->shoeSize != '')
+ $additionalData['shoe_size'] = $optionLookup['shoe_size'][(string)$product->shoeSize];
+ if ($product->signature != '')
+ $additionalData['signature'] = $optionLookup['signature'][(string)$product->signature];
+
+ // Literal attributes:
+ // bust_size, cost, dress_length, heels_height,
+ // width, height, length, local_price, local_sku,
+ // net_height, net_length, net_weight, net_width,
+ // shawl_length, shawl_width,
+ // shoe_measurement, shop_id, waist_size
+ if ($product->bust_size != '')
+ $additionalData['bust_size'] = (string)$product->signature;
+ if ($product->cost != '')
+ $additionalData['cost'] = (string)$product->cost;
+ if ($product->dress_length != '')
+ $additionalData['dress_length'] = (string)$product->dressLength;
+ if ($product->heels_height != '')
+ $additionalData['heels_height'] = (string)$product->heelsHeight;
+ if ($product->width != '')
+ $additionalData['width'] = (string)$product->width;
+ if ($product->height != '')
+ $additionalData['height'] = (string)$product->height;
+ if ($product->length != '')
+ $additionalData['length'] = (string)$product->length;
+ if ($product->localPrice != '')
+ $additionalData['local_price'] = (string)$product->localPrice;
+ if ($product->localSku != '')
+ $additionalData['local_sku'] = (string)$product->localSku;
+ if ($product->netHeight != '')
+ $additionalData['net_height'] = (string)$product->netHeight;
+ if ($product->netLength != '')
+ $additionalData['net_length'] = (string)$product->netLength;
+ if ($product->netWeight != '')
+ $additionalData['net_weight'] = (string)$product->netWeight;
+ if ($product->netWidth!= '')
+ $additionalData['net_width'] = (string)$product->netWidth;
+ if ($product->shawlLength != '')
+ $additionalData['shawl_length'] = (string)$product->shawlLength;
+ if ($product->shawlWidth != '')
+ $additionalData['shawl_width'] = (string)$product->shawlWidth;
+ if ($product->shoeMeasurement != '')
+ $additionalData['shoe_measurement'] = (string)$product->shoeMeasurement;
+ if ($product->shopId != '')
+ $additionalData['shop_id'] = (string)$product->shopId;
+ if ($product->waistSize != '')
+ $additionalData['waistSize'] = (string)$product->waistSize;
+
+ var_dump($additionalData);
echo "Create simple product $sku name: $name price: $price qty: $qty cats: $cats webs: $webs\n";
createSimpleProduct(array(
'storeId' => $storeId,
@@ -238,7 +317,10 @@
'weight' => $weight,
'price' => $price,
'categoryIds' => $categoryIds,
- 'websiteIds' => $websiteIds));
+ 'websiteIds' => $websiteIds,
+ 'qty' => 1),
+ $additionalData);
+ exit(0);
} else if ($product->type == 'configurable') {
echo "Create configurable product $sku name: $name set: $set price: $price variants: $variants cats: $cats webs: $webs\n";
$variantCodes = explode(',', $variants);

0 comments on commit 2b2b89f

Please sign in to comment.
Something went wrong with that request. Please try again.