Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Trying to understand Create product API
 
angelo_f
Sr. Member
 
Total Posts:  80
Joined:  2011-10-28
 

I want to programatically add an item to magento and have found the following example

http://www.fontis.com.au/blog/magento/creating-magento-products-script

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

$product->setSku("ABC123");
$product->setName("Type 7 Widget");
$product->setDescription("This widget will give you years of trouble-free widgeting.");
$product->setShortDescription("High-end widget.");
$product->setPrice(70.50);
$product->setTypeId(’simple’);
$product->setAttributeSetId(9); // need to look this up
$product->setCategoryIds("20,24"); // need to look these up
$product->setWeight(1.0);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled

// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$product->save();

I can’t work out what the following is for

$product->setAttributeSetId(9); // need to look this up

What does it actually do?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
Each product can have different attributes.
For example you can sell in the same website, shoes and computers. smile
The shoes will have size & gender but the computer does not need these attributes and they need, processor, HDD (you get the point).
In order to achieve this Magento allows you to create different attribute sets so when adding a product you are able to edit the right attributes.
So you can have the ‘Shoes’ attribute set and the ‘Computer’ attribute set and you can assign them to the product.
If all your products have the same attributes you still need an attribute set. For this there is the ‘Default’ set.

So basically by this line

$product->setAttributeSetId(9);
You define the product attributes. (it’s a mandatory field). You can later add or remove attributes from the set without having to reconfigure your products.

I hope I was clear enough.
Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
angelo_f
Sr. Member
 
Total Posts:  80
Joined:  2011-10-28
 

That makes some sense, but where do I see the value that equates to 9.
For the setCategoryIds(\"20,24\"), I can see in the backend that the categories map to an ID - eg, with the demo data I can see Furniture (ID: 10)

But for attribute sets, I can\’t find anywhere that the number maps to

 
Magento Community Magento Community
Magento Community
Magento Community
 
angelo_f
Sr. Member
 
Total Posts:  80
Joined:  2011-10-28
 

Actually, I worked it out. I had a look through the tables and saw how it was defined

To get the ID for a attribute set that I have defined, I do

$attrSetName = ‘myattset;
$attributeSetId = Mage::getModel(’eav/entity_attribute_set’)
->load($attrSetName, ‘attribute_set_name’)
->getAttributeSetId();

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
ravimadan86
Jr. Member
 
Total Posts:  3
Joined:  2012-08-29
 

@tzyganu

look at the ‘attribute_set_id’ column in “catalog_product_entity” table

This will help you with the setting $product->setAttributeSetId(4) value

attribute_set_id can be found in “eav_attribute_set” table

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top