Saturday, 26 January 2013

Programmatically add attributes's option in magento



$arg_attribute = '<attribute-code>';
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
$opt['attribute_id'] = $attr_id;
$opt['value']['<admin-value>'][0] = '<admin-name>'; //admin name of option
$opt['value']['<admin-value>']][1] = '<admin-name>'; //default store name of option

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($opt);

Programmatically create a category within another category in magento


//$parentId is the category id of a category under which you want to want add new category.


$category = Mage::getModel('catalog/category');
$category->setName('name of new category')
->setIsActive(1)                       //activate your category
->setDisplayMode('PRODUCTS')
->setIsAnchor(1)
->setCustomDesignApply(1)
->setAttributeSetId($category->getDefaultAttributeSetId());

$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());

$category->save();
unset($category);


That's all.. Enjoy.. :)