Question

I am facing a problem regarding creating attribute and attribute set using installer script.attribute set and attribute is created but the problem is attributes are assigned to all attribute set instead of only custom one creating with installer script.

following is my installer script:

<?php
$installer = $this;
$installer->startSetup();
try{
$sNewSetName = 'Product Bundler Package';
$iCatalogProductEntityTypeId = (int) $installer->getEntityTypeId('catalog_product');

$oAttributeset = Mage::getModel('eav/entity_attribute_set')
    ->setEntityTypeId($iCatalogProductEntityTypeId)
    ->setAttributeSetName($sNewSetName);

if ($oAttributeset->validate()) {
    $oAttributeset
        ->save()
        ->initFromSkeleton($installer->getAttributeSetId('catalog_product', 'Default'))
        ->save();
}
else {
    Mage::log('Attributeset with name ' . $sNewSetName . ' already exists.');
    }
}
catch(Exception $ex){
Mage::log('Attributeset with name ' . $sNewSetName . ' already exists.');

}


    $installer->addAttributeGroup('catalog_product', 'Product Bundler Package', 'Bundled Package Data', 1000);

    $data1= array (
        'attribute_set' =>  'Product Bundler Package',
        'group' => 'Bundled Package Data',
        'label'    => 'Preset1 name',
        'visible'     => true,
        'type'     => 'varchar', 
        'input'    => 'text',
        'system'   => true,
        'required' => false,
        'user_defined' => 1, 
    );

    $installer->addAttribute('catalog_product','bundle_preset1_name',$data1);

      $data2= array (
        'attribute_set' =>  'Product Bundler Package',
        'group'    => 'Bundled Package Data',
        'label'    => 'Preset2 name',
        'visible'  => true,
        'type'     => 'varchar', 
        'input'    => 'text',
        'system'   => true,
        'required' => false,
        'user_defined' => 1, 
    );
    $installer->addAttribute('catalog_product','bundle_preset2_name',$data2);

    $data3= array (
        'attribute_set' =>  'Product Bundler Package',
        'group' => 'Bundled Package Data',
        'label'    => 'Preset3 name',
        'visible'     => true,
        'type'     => 'varchar', 
        'input'    => 'text',
        'system'   => true,
        'required' => false,
        'user_defined' => 1, 
    );
    $installer->addAttribute('catalog_product','bundle_preset3_name',$data3);

    $data4 = array (
        'attribute_set' =>  'Product Bundler Package',
        'group' => 'Bundled Package Data',
        'label'    => 'Preset4 name',
        'visible'     => true,
        'type'     => 'varchar', 
        'input'    => 'text',
        'system'   => true,
        'required' => false,
        'user_defined' => 1, 
    );
   $attribute =  $installer->addAttribute('catalog_product','bundle_preset4_name',$data4);


$installer->endSetup();
?>

I want to create custom attributes with attribute set name "Product Bundler Package" and assign all attribute to that attribute set only.

Kindly help me to resolve it.

Was it helpful?

Solution

Not much clear what you want to do really. Anyway this is the script to create a product attribute after you created the attribute set.

<?php

$th =  new Mage_Catalog_Model_Resource_Setup();  
$th->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'your_custom_attribute_code', array(
            'group' => 'Prices', 
            'type' => 'text',
                        'attribute_set' =>  'Default', // Your custom Attribute set
            'backend' => '',
            'frontend' => '',
            'label' => 'My Custom Attribute',
            'input' => 'text',
            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '1',
            'searchable' => false,
            'filterable' => true,
            'comparable' => false,
            'visible_on_front' => true,
            'visible_in_advanced_search' => true,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => 'simple',  // Apply to simple product type
        ) );

Note : For further info please refer http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/programmatically_adding_attributes_and_attribute_sets

OTHER TIPS

Basically just like answer by @Sukeshini but you need to leave group undefined in the attributes data and specify user_defined as true.

Then it will not be added to any attribute sets. AFAIK you cannot define the attribute set when creating the attribute in this method. You can add the attribute to an attribute set after creating it in the upgrade or setup script.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top