1

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.

  • is anyone there to resolve it???? – Keyur Patel Nov 8 '13 at 7:16
  • what is the actual problem, do you need code to create the attribute set or code to add the attributes to this new set? – David Manners Nov 8 '13 at 8:07
  • David, i want to create custom attributes within custom attribute set using installer script. i am able to create attributes but it is set to all attribute set. – Keyur Patel Nov 11 '13 at 5:58
5

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

  • i have tried this but it is also not working. attributes are added to all attribute set using this. i want to add in custom attribute set only. – Keyur Patel Nov 8 '13 at 13:21
  • @KeyurPatel is it working fine? – manokarthick karthick Dec 23 '16 at 13:48
  • i don't know exactly what i did because its been so long time since i asked the question. – Keyur Patel Dec 26 '16 at 10:25
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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