0

I am new to Magento and I am creating a product attribute but the problem I have is that I have 3 Attribute Sets (default, Imported, Local), but when I create the attribute, this attribute is created in the 3 Attribute Sets, even if I do not have the created group, the attribute creates it automatically.

$this->startSetup();    
$this->addAttribute('catalog_product', 'cod_import', array(
    'label'             => 'Código de Importación',
    'attribute_set'     => 'Importados',
    'group'             => 'Especificaciones de importacion',
    'type'              => 'decimal',
    'visible'           => true,
    'required'          => true,
    "nullable"          => true,
    'sort_order'        => 2
));
$this->endSetup();

2 Answers 2

0

You cannot define the attribute set when creating the attribute with this method. Change group to undefined and set user_defined as true to create an attribute without setting an attribute set. After creating it, add the attribute to an attribute set in the upgrade or setup script.

0

You can do it as part of install script


/** 
* Add blank attibute set 
*/ 
$attibuteSet = new Mage_Eav_Model_Entity_Setup('core_setup'); 
/* @var $attibuteSet Mage_Eav_Model_Entity_Setup */ 
$attibuteSet->startSetup(); 
$attibuteSet->addAttributeSet( 
    Mage_Catalog_Model_Product::ENTITY, 'Custom' 
); 
$attibuteSet->endSetup(); 


/** 
* Add attibute set based on default set 
*/ 
$attibuteSet = new Mage_Catalog_Model_Resource_Setup('core_setup'); 
$attibuteSet->startSetup(); 
$defaultAttributeSetId  = $attibuteSet->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, 'Default'); 
$entityTypeId           = $attibuteSet->getEntityTypeId(Mage_Catalog_Model_Product::ENTITY); 
$attributeSetsName      = array('CustomTwo'); 


foreach ($attributeSetsName as $attributeSetName) { 

    $model  = Mage::getModel('eav/entity_attribute_set')->setEntityTypeId($entityTypeId); 
    $model->setAttributeSetName($attributeSetName); 


try { 
        if ($model->validate()) { 
            $model->save(); 
            $model->initFromSkeleton($defaultAttributeSetId); 
            $model->save(); 
        } 
    } catch (Exception $e) { 
        Mage::logException($e); 
    } 
} 
$attibuteSet->endSetup(); 


/** 
* Add Attibute 
*/ 
$addAttribute = new Mage_Catalog_Model_Resource_Setup('core_setup'); 
/* @var $attribute Mage_Catalog_Model_Resource_Setup */ 
$productTypes = array( 
    Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, 
    Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, 
); 


$addAttribute->startSetup(); 


if (!$addAttribute->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'custom_attribute')) { 
    $addAttribute->addAttribute( 
        Mage_Catalog_Model_Product::ENTITY,  
        'custom_attribute',  
            array(          // TABLE.COLUMN:                                       DESCRIPTION: 
            'label'                      => 'Custom Attribute',                                       // eav_attribute.frontend_label                        admin input label 
            'group'                      => 'General',                                                // (not a column)                                      tab in product edit screen 
            'sort_order'                 => 0,                                                        // eav_entity_attribute.sort_order                     sort order in group 
            'backend'                    => '',                                                       // eav_attribute.backend_model                         backend class (module/class_name format) 
            'type'                       => 'varchar',                                                // eav_attribute.backend_type                          backend storage type (varchar, text etc) 
            'frontend'                   => '',                                                       // eav_attribute.frontend_model                        admin class (module/class_name format) 
            'note'                       => null,                                                     // eav_attribute.note                                  admin input note (shows below input) 
            'default'                    => null,                                                     // eav_attribute.default_value                         admin input default value 
            'wysiwyg_enabled'            => false,                                                    // catalog_eav_attribute.is_wysiwyg_enabled            (products only) admin input wysiwyg enabled 
            'input'                      => 'text',                                                   // eav_attribute.frontend_input                        admin input type (select, text, textarea etc) 
            'input_renderer'             => false,                                                    // catalog_eav_attribute.frontend_input_renderer       (products only) admin input renderer (otherwise input is used to resolve renderer) 
            'source'                     => null,                                                     // eav_attribute.source_model                          admin input source model (for selects) (module/class_name format) 
            'required'                   => false,                                                    // eav_attribute.is_required                           required in admin 
            'user_defined'               => true,                                                     // eav_attribute.is_user_defined                       editable in admin attributes section, false for not 
            'unique'                     => false,                                                    // eav_attribute.is_unique                             unique value required 
            'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,  // catalog_eav_attribute.is_global                     (products only) scope 
            'visible'                    => true,                                                     // catalog_eav_attribute.is_visible                    (products only) visible on admin 
            'visible_on_front'           => false,                                                    // catalog_eav_attribute.is_visible_on_front           (products only) visible on frontend (store) attribute table 
            'used_in_product_listing'    => false,                                                    // catalog_eav_attribute.used_in_product_listing       (products only) made available in product listing 
            'searchable'                 => false,                                                    // catalog_eav_attribute.is_searchable                 (products only) searchable via basic search 
            'visible_in_advanced_search' => false,                                                    // catalog_eav_attribute.is_visible_in_advanced_search (products only) searchable via advanced search 
            'filterable'                 => false,                                                    // catalog_eav_attribute.is_filterable                 (products only) use in layered nav 
            'filterable_in_search'       => false,                                                    // catalog_eav_attribute.is_filterable_in_search       (products only) use in search results layered nav 
            'comparable'                 => false,                                                    // catalog_eav_attribute.is_comparable                 (products only) comparable on frontend 
            'is_html_allowed_on_front'   => false,                                                    // catalog_eav_attribute.is_visible_on_front           (products only) seems obvious, but also see visible 
            'apply_to'                   => join(',', $productTypes),                                 // catalog_eav_attribute.apply_to                      (products only) which product types to apply to 
            'is_configurable'            => true,                                                     // catalog_eav_attribute.is_configurable               (products only) used for configurable products or not 
            'used_for_sort_by'           => false,                                                    // catalog_eav_attribute.used_for_sort_by              (products only) available in the 'sort by' menu 
            'position'                   => 0,                                                        // catalog_eav_attribute.position                      (products only) position in layered naviagtion 
            'used_for_promo_rules'       => false,                                                    // catalog_eav_attribute.is_used_for_promo_rules       (products only) available for use in promo rules 
        ) 
    ); 
} 


$addAttribute->endSetup(); 


/** 
* Add Attibute To Set 
*/ 


$addAttributeToSet = new Mage_Catalog_Model_Resource_Setup('core_setup'); 
/* @var $addAttributeToSet Mage_Catalog_Model_Resource_Setup */ 


$attributeId        = $addAttributeToSet->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'custom_attribute'); 
$attributeSetId     = $addAttributeToSet->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, 'Custom'); 
$attributeGroupId   = $addAttributeToSet->getAttributeGroup(Mage_Catalog_Model_Product::ENTITY, $attributeSetId, 'General'); 
$addAttributeToSet->addAttributeToSet( 
    Mage_Catalog_Model_Product::ENTITY,  
    $attributeSetId,  
    $attributeGroupId,  
    $attributeId 
); 


$addAttributeToSet->endSetup(); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.