欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

add attribute group and create new attribute to this group

程序员文章站 2022-06-04 11:06:44
...

//for example:

// I will add a attribute group named 'Vendor' to 'Default' attribute set

function addAttributeGroup($group_name, $attrSetName,$sort_order) {
	$modelGroup = Mage::getModel('eav/entity_attribute_group');
	$attributeSetId = Mage::getModel('eav/entity_attribute_set')
	->load($attrSetName, 'attribute_set_name')
	->getAttributeSetId();
	$modelGroup->setAttributeGroupName($group_name) //change group name
	//link to the current set
	->setAttributeSetId($attributeSetId)
	//set the order in the set
	->setSortOrder($sort_order);
	//save the new group
	$modelGroup->save();
}

addAttributeGroup('Vendor', 'Default',100);

 //Then Create a Select drop down attribute and add it to the group

function createSelectAttribute($code, $label, $group_name )
{
	$_attribute_data = array(
			'type'       => 'int',
			'input'      => 'select',
			'label'      => $label,
			'sort_order' => 1000,
			'required'   => false,
			'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
			'backend'    => 'eav/entity_attribute_backend_array',
			'option'     => array (
					'values' => array(
					)
			),
			'group' => $group_name,
			'visible' => true,
			'visible_on_front'=>true,
			'unique' => false,
			'is_configurable' => false,
			'used_for_promo_rules' => true,
	);
	
	$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
	$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $code);
	if ($attrIdTest === false) {
		$objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $code, $_attribute_data);
	}
}

//add vendor name
createSelectAttribute('vendor_name','Vendor Name','Vendor');
//add vendor no
createSelectAttribute('vendor_no','Vendor Number','Vendor');

 //at Last i write a text format attribute function

function createTextAttribute($code, $label, $group_name )
{
	$_attribute_data = array(
			'type'       => 'varchar',
			'input'      => 'text',
			'label'      => $label,
			'required'   => false,
			'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
			'backend'    => '',
			'group' => $group_name,
			'visible' => true,
			'visible_on_front'=>true,
			'unique' => false,
			'is_configurable' => false,
			'used_for_promo_rules' => true
	);
	$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
	$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $code);
	if ($attrIdTest === false) {
		$objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $code, $_attribute_data);
	}
}

//add vendor item no
createTextAttribute('vendor_item_no','Vendor Item Number','Vendor');