Skip to content

Magento : Add New Attribute & Attribute Set Programatically

Here short script to add new attributes & attributes set

 

<?php  

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();


function validateAttr($attr_name)
{
	$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
				->setCodeFilter($attr_name)
				->getFirstItem();
		$attCode = $attributeInfo->getAttributeCode();
		return $attributeInfo->getId();
}

function validateAttributeSets($attrs_name)
{
	
	$data = Mage::getModel('eav/entity_attribute_set')->getCollection()
		->addFieldToFilter('attribute_set_name',$attrs_name)
		->addFieldToFilter('entity_type_id','4')
		->getFirstItem()
		;
		
	if($data->getId())
	{
		return $data->getId();
	}
		
		$buat_baru = array(

		'entity_type_id'=>4,
		'attribute_set_name'=>$attrs_name,


		);
		try{
			 //$data = Mage::getModel('eav/entity_attribute_set')->setData($buat_baru)->save();
			 
			 $entityTypeId = Mage::getModel('catalog/product')
                  ->getResource()
                  ->getEntityType()
                  ->getId(); //product entity type

			$attributeSet = Mage::getModel('eav/entity_attribute_set')
							  ->setEntityTypeId($entityTypeId)
							  ->setAttributeSetName($buat_baru['attribute_set_name']);

			$attributeSet->validate();
			$attributeSet->save();

			$attributeSet->initFromSkeleton(4)->save();
			 
			 
			 return $attributeSet->getId();
		}catch(Exception $xx)
		{
			echo 'gagal buat attribute sets "'.$buat_baru['attribute_set_name'].'" <br>';
		}
}

function createAttribute($label, $attribute_type, $product_type,$attribute_code,$attribute_group)
{	

	$attribute_code = substr($attribute_code,0,30);
	$attribute_code_cek = validateAttr($attribute_code);
	$attId = $attribute_code_cek;
	if(!$attribute_code_cek):
		
	$_attribute_data = array(
		'attribute_code' => /*'old_site_attribute_'.(($product_type) ? $product_type : 'joint').'_'.$code*/ $attribute_code,
		'is_global' => '1',
		'frontend_input' => $attribute_type, //'boolean',
		'default_value_text' => '',
		'default_value_yesno' => '0',
		'default_value_date' => '',
		'default_value_textarea' => '',
		'is_unique' => '0',
		'is_required' => '0',
		'apply_to' => array($product_type), //array('grouped')
		'is_configurable' => '0',
		'is_searchable' => '0',
		'is_visible_in_advanced_search' => '0',
		'is_comparable' => '0',
		'is_used_for_price_rules' => '0',
		'is_wysiwyg_enabled' => '0',
		'is_html_allowed_on_front' => '1',
		'is_visible_on_front' => '0',
		'used_in_product_listing' => '0',
		'used_for_sort_by' => '0',
		'frontend_label' => $label
	);
 
 
	$model = Mage::getModel('catalog/resource_eav_attribute');
 
	if (!isset($_attribute_data['is_configurable'])) {
		$_attribute_data['is_configurable'] = 0;
	}
	
	
	
	if (!isset($_attribute_data['is_filterable'])) {
		$_attribute_data['is_filterable'] = 0;
	}
	
	
	if($attribute_type == 'select')
	{
		$_attribute_data['is_configurable'] = 1;
		$_attribute_data['is_filterable'] = 1;
	}
	
	if (!isset($_attribute_data['is_filterable_in_search'])) {
		$_attribute_data['is_filterable_in_search'] = 0;
	}
 
	if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
		$_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
	}
 
	$defaultValueField = $model->getDefaultValueByInput($_attribute_data['frontend_input']);
	if ($defaultValueField) {
		//$_attribute_data['default_value'] = $this->getRequest()->getParam($defaultValueField);
	}
 
 
	$model->addData($_attribute_data);
 
	$model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
	$model->setIsUserDefined(1);
 
 
	try {
		$model->save();
		
		
		/*set attribute nya ke attribute group*/
		
		$attId = $model->getId();
		
		
		echo 'Attribute '.$label.' done <br>';
	} catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute '.$label.'. Error: '.$e->getMessage().'</p>'; }
	
	endif;
	
	
	try{
		
		if($attId):
		$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem();
		$SID = validateAttributeSets($attribute_group);
		
		$set = Mage::getModel('eav/entity_attribute_set')->load($SID);
				$setId = $set->getId();
		
		$group = Mage::getModel('eav/entity_attribute_group')->getCollection()
				->addFieldToFilter('attribute_set_id',$setId)
				->addFieldToFilter('attribute_group_name', 'General')
				
				->setOrder('attribute_group_id',ASC)->getFirstItem();
				$groupId = $group->getId();
				$newItem = Mage::getModel('eav/entity_attribute');
				$newItem->setEntityTypeId($attSet->getId())
						  ->setAttributeSetId($setId)
						  ->setAttributeGroupId($groupId)
						  ->setAttributeId($attId) 
						  //->setSortOrder(SORT ORDER) 
						  ->save();
		echo 'Attribute '.$label.' to '.$attribute_group.' done <br>';
		endif;
	}catch(Exception $xx)
	{
		echo $SID.' fail to set attribute sets '.$xx->getMessage();
		var_dump($SID);
	}
	
}


createAttribute('Attribute Label 1', 'select', 'simple,grouped,configurable,virtual,bundle,downloadable,giftcard','attr_code1','New Attribute Sets 1');
createAttribute('Attribute Label 2', 'simple,grouped,configurable,virtual,bundle,downloadable,giftcard','attr_code2','New Attribute Sets 1');

 

Share

Comments are closed.