Add dropdown product attribute and add it to all attribute set

127 篇文章 0 订阅

config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <Bysoft_Import>
      <version>0.1.1</version>
    </Bysoft_Import>
  </modules>
  <global>
    <helpers>
      <import>
        <class>Bysoft_Import_Helper</class>
      </import>
    </helpers>
	<models>
	  <import>
		<class>Bysoft_Import_Model</class>
		<resourceModel>import_mysql4</resourceModel>
	  </import>
	  <import_mysql4>
		<class>Bysoft_Import_Model_Mysql4</class>
		<entities>		  
			  <product>
				<table>bysoft_import_product</table>
			  </product>		  
			  <category>
				<table>bysoft_import_category</table>
			  </category>
        </entities>
	  </import_mysql4>
	</models>
	<resources>
	  <import_setup>
		<setup>
		  <module>Bysoft_Import</module>
		  <class>Mage_Eav_Model_Entity_Setup</class>
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </import_setup>
	  <import_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </import_write>
	  <import_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </import_read>
	</resources>
  </global>
</config> 

 

upgrade script:

<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', "specific_usage", array(
		'type'       => 'int',
		'input'      => 'select',
		'label'      => 'Specific Usage',
		'sort_order' => 1000,
		'required'   => false,
		'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
		'backend'    => 'eav/entity_attribute_backend_array',
		'option'     => array (
				'values' => array(
						
				)
		),

));

$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem(); // This is because the you adding the attribute to catalog_products entity ( there is different entities in magento ex : catalog_category, order,invoice... etc )
$attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); // this is the attribute sets associated with this entity
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('specific_usage')
->getFirstItem();
$attCode = $attributeInfo->getAttributeCode();
$attId = $attributeInfo->getId();
foreach ($attSetCollection as $a)
{
	$set = Mage::getModel('eav/entity_attribute_set')->load($a->getId());
	$setId = $set->getId();
	$group = Mage::getModel('eav/entity_attribute_group')->getCollection()->addFieldToFilter('attribute_set_id',$setId)->setOrder('attribute_group_id',ASC)->getFirstItem();
	$groupId = $group->getId();
	$newItem = Mage::getModel('eav/entity_attribute');
	$newItem->setEntityTypeId($attSet->getId()) // catalog_product eav_entity_type id ( usually 10 )
	->setAttributeSetId($setId) // Attribute Set ID
	->setAttributeGroupId($groupId) // Attribute Group ID ( usually general or whatever based on the query i automate to get the first attribute group in each attribute set )
	->setAttributeId($attId) // Attribute ID that need to be added manually // Sort Order for the attribute in the tab form edit
	->save()
	;
	//echo "Attribute ".$attCode." Added to Attribute Set ".$set->getAttributeSetName()." in Attribute Group ".$group->getAttributeGroupName()."<br>\n";
}

$installer->endSetup();
	 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值