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

Magento create new product attribute and create new attribute group

程序员文章站 2022-03-03 22:26:19
...

1.add new product attribute

<?php 
//增加一个是否预售的属性,到所有商品
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');

//添加jd URL
$attrCode = 'jd_url';
$label = 'JD Url';
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode);
if ($attrIdTest === false) {
	$this->addAttribute( Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
			'sort_order' => 8,
			'type' => 'varchar',
			'backend' => '',
			'frontend' => '',
			'label' => $label,
			'input' => 'text',
			'source' => 'eav/entity_attribute_source_table',
			'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
			'visible' => true,
			'required' => false,
			'user_defined' => true,
			'default' => '',
			'searchable' => false,
			'filterable' => false,
			'comparable' => false,
			'visible_on_front' => true,
			'visible_in_advanced_search' => false,
			'used_in_product_listing' => true,
			'used_for_promo_rules'=>false,
			'unique' => false,
	) );
}

//添加AMAZON URL
$attrCode = 'amazon_url';
$label = 'Amazon Url';
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode);
if ($attrIdTest === false) {
	$this->addAttribute( Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
			'sort_order' => 9,
			'type' => 'varchar',
			'backend' => '',
			'frontend' => '',
			'label' => $label,
			'input' => 'text',
			'source' => 'eav/entity_attribute_source_table',
			'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
			'visible' => true,
			'required' => false,
			'user_defined' => true,
			'default' => '',
			'searchable' => false,
			'filterable' => false,
			'comparable' => false,
			'visible_on_front' => true,
			'visible_in_advanced_search' => false,
			'used_in_product_listing' => true,
			'used_for_promo_rules'=>false ,
			'unique' => false,
	) );
}

$installer->endSetup();

 2. add attribute group and append new attribute to it.

<?php 
//增加一个是否预售的属性,到所有商品
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

//$attribute_set_name = 'your attribute set name here';
$group_name = 'External Website Link';
$attribute_code_arr = array('jd_url', 'amazon_url');


//add group to all attribute set 
//get the type ID of the product - you will need it later
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
//get all attribute sets
$sets = Mage::getModel('eav/entity_attribute_set')
->getResourceCollection()
//filter only sets for products - that's why you needed the product type ID
->addFilter('entity_type_id', $entityTypeId);
//loop through all the sets
foreach ($sets as $set){
	//create an attribute group instance
	$modelGroup = Mage::getModel('eav/entity_attribute_group');
	//set the group name
	$modelGroup->setAttributeGroupName($group_name) //change group name
	//link to the current set
	->setAttributeSetId($set->getId())
	//set the order in the set
	->setSortOrder(100);
	//save the new group
	$modelGroup->save();
}

$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')->load();
foreach ($attributeSetCollection as $id=>$attributeSet) {
	$attribute_set_name = $attributeSet->getAttributeSetName();
	foreach ($attribute_code_arr as $attribute_code) {
		$attribute_set_id=$setup->getAttributeSetId('catalog_product', $attribute_set_name);
		$attribute_group_id=$setup->getAttributeGroupId('catalog_product', $attribute_set_id, $group_name);
		$attribute_id=$setup->getAttributeId('catalog_product', $attribute_code);
		$setup->addAttributeToSet('catalog_product',$attribute_set_id, $attribute_group_id, $attribute_id);
	}
	
}

$installer->endSetup();

 

相关标签: add