Wednesday, November 25, 2009

Import Simple Products (for barrmy)

You don't need to import the data directly in the into the tables. My suggestion is to use the models provided by magento (that's why it's a MVC).

Create an product instance like this:

$product = Mage::getModel('catalog/product');

set the data you need
$product->setName('PRODUCT NAME');
$product->setDescription('PRODUCT DESCRIPTION');
and so on.
To add stock to this:
$stock_data=array(
'use_config_manage_stock' => 1,
'qty' => 998,
'min_qty' => 0,
'use_config_min_qty'=>1,
'min_sale_qty' => 0,
'use_config_min_sale_qty'=>1,
'max_sale_qty' => 9999,
'use_config_max_sale_qty'=>1,
'is_qty_decimal' => 0,
'backorders' => 0,
'notify_stock_qty' => 0,
'is_in_stock' => 1
);
$product->setData('stock_data',$stock_data);

If you have more that 1 website you must specify on which website to appear
$product->setWebsiteIds(array(1,2));
To add categories:
$product->setCategoryIds(array(7,10));
Set status
$product->setStatus(1);//1=Enabled; 2=Disabled;
Set visibility
$product->setVisibility(4);//4 = catalog & search.
For photos:
$product->addImageToMediaGallery('path/to/photo', array('image', 'small_image', 'thumbnail'), false, false);

second parameter can be replaced by null for a regular image. You can test the difference :)

After all the attributes are set just call
$product->save();
and Magento will put all the attributes in the right table.
If you want to update a product, do the same but instead of creating an "empty" instance of a product, load one.
$product = Mage::getModel('catalog/product')->load(PRODUCT ID);

This will work if you want to modify the default values. If you want to modify the values for a specific store do this.
$product = Mage::getModel('catalog/product')->setStoreId(STORE ID)->load(PRODUCT ID);
These are the basics. If you want a "fine tuned" product creation you just have to play around with the attributes setData() method.

It's not easy. You need some programming skills, but it can be done.

You can find an update to this post here:
http://marius-strajeru.blogspot.com/2010/05/import-products-update.html

1 comment:

  1. Hi,

    Thank you for linking my site here :), I will also do the same for you lol :) Would you like to become an editor on my site? helping people around with Magento? Reply me back at mike@maghelpdesk.com if you would like to help a hand.

    ReplyDelete