通过编程将“自定义”属性添加到“自定义”属性集中


10

嗨,有人可以帮我吗?

我创建了一个自定义属性集和一个自定义属性

$installer = $this;
/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer->startSetup();

//Create Attribute set with Based on Default attribute set
//$installer->removeAttributeSet(Mage_Catalog_Model_Product::ENTITY, 'New Attr Set');
/*
$skeletonID=$installer->getAttributeSetId('catalog_product','Default');
$entityTypeId = Mage::getModel('catalog/product')
->getResource()
->getEntityType()
->getId(); //product entity type

$attributeSet = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId($entityTypeId)
->setAttributeSetName("New Attr Set");

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

$attributeSet->initFromSkeleton($skeletonID)->save();


//Create attribute new_attr
//$installer->removeAttribute('catalog_product', 'new_attr');
$data= array (
'attribute_set' =>  'New Attr Set',
'group' => 'General',
'label'    => 'New Attr',
'visible'     => true,
'type'     => 'int', // multiselect uses comma-sep storage
'input'    => 'boolean',
'system'   => false,
'required' => false,
'user_defined' => 1,//defaults to false; if true, define a group
'source' => 'eav/entity_attribute_source_boolean',
'default' => 1,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
 );

 $installer->addAttribute('catalog_product','new_attr',$data);
 */

此代码将修饰符“ new_attr”添加到组“常规”中,因此自定义属性也显示在所有属性集中,例如“默认”。

我只想将自定义属性“ new_attr”添加到“常规”组下的自定义属性集“新属性集”。那可能吗?


这有可能实现吗?
Magento_Newbie 2014年

Answers:


18

是的,这是可能的。

首先,将$ data数组中的这些键设置为以下值,以避免将属性添加到所有属性集:

'user_defined'         => true,
'group'                => ''

然后将属性添加到您的属性集:

$attributeSetId = $this->getAttributeSetId($entityTypeId, 'New Attr Set');
$this->addAttributeToSet($entityTypeId, $attributeSetId, 'General', 'new_attr', 10);

0

我的功能(通过代码)将属性添加到属性集

    public function addToAttributeSet($code, $attributeSetName = 'Default', $groupName = 'Details')
{
    try {
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

        $attributeSetId = $setup->getAttributeSetId('catalog_product', $attributeSetName);
        $attributeGroupId = $setup->getAttributeGroupId('catalog_product', $attributeSetId, $groupName);
        $attributeId = $setup->getAttributeId('catalog_product', $code);

        $setup->addAttributeToSet($entityTypeId = 'catalog_product', $attributeSetId, $attributeGroupId, $attributeId);

        $this->_success[] = 'Added Attribute to SET ' . $attributeSetName . ' (' . $code . ')';
        return true;

    } catch (Exception $e) {
        $this->_errors[] = 'ERROR when added Attribute to SET ' . $attributeSetName . ' (' . $code . ')';
        $this->_errors[] = $e->getMessage();
        return false;
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.