cancel
Showing results for 
Search instead for 
Did you mean: 

Add attributes and their values to existing attribute set programmatically

Add attributes and their values to existing attribute set programmatically

0

I want to add 2 groups and their attributes programmatically to an existing Set Group This is the screenshot.

 

Please note that I am willing to add two Groups each one containing 3 attributes the first one and 4 the second one. * The groups are not exisiting, Only the Attribute Set exist This is my essay :

<?php

/* @var $conn Varien_Db_Adapter_Interface */
/* @var $installer Mage_Catalog_Model_Resource_Setup */$installer = $this;$installer->startSetup();

/**
 * Entities
 */$productEntity = Mage_Catalog_Model_Product::ENTITY;
$installer->addAttribute($productEntity, 'gen2', array(
    'type'             => 'int',
    'group'            => 'General2',
    'backend'          => '',
    'frontend'         => '',
    'label'            => 'label1',
    'input'            => 'select',
    'class'            => '',
    'note'             => '',
    'global'           => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source'           => "eav/entity_attribute_source_table",
    'apply_to'         => array('simple', 'configurable', 'bundle'),
    'visible'          => true,
    'required'         => false,
    'user_defined'     => true,
    'default'          => '0',
    'searchable'       => false,
    'filterable'       => false,
    'comparable'       => false,
    'visible_on_front' => true,
    'unique'           => false,
));$installer->endSetup();$attribute = Mage::getSingleton('eav/config')->getAttribute($productEntity, 'gen2');$options = array(
    "0",
    "1",
    "2",
    "3",
);

foreach ($options as $key => $option) {    $installer->addAttributeOption(array(
        'attribute_id' => $attribute->getId(),
        'value' => array(
            "option-" . $key => array(0 => $option)
        )
    ));
}

Please note that only the group set (like Attset in the image) is existing and the groups and their attributes are inexistant. Also should I run the script from shell command? Thanks in advance

2 REPLIES 2

Re: Add attributes and their values to existing attribute set programmatically

I use a lib project which views I want to create programmatically, not via XML. When creating its layout via XML, I need to set an id for which there is no setter available in Java. Is there any chance to create this view with all its properties in Java? Example:
<de.libproject.View android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="wrap_content" lib:loadImages="true" lib:emptyView="true" lib:imageId="15696" />

https://snaptube.cam/ , https://vidmate.bid/

Re: Add attributes and their values to existing attribute set programmatically

Hello @ahmed_chouihi ,

 

If you assigned any attribute to attribute set then you will not lost your attribute value.

 

You can assign by using following code

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
 
/* Assign specific product attribute to any single attribute set */
$attributeCode = 'size';
$attributeGroup = 'Product Details';
$attributeSet = 'Bag';
 
$eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
$config = $objectManager->get(\Magento\Catalog\Model\Config::class);
$attributeManagement = $objectManager->get(\Magento\Eav\Api\AttributeManagementInterface::class);
 
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
 
/* Get attribute set information by using attribute set name */
$attributeSet = $eavSetup->getAttributeSet($entityTypeId, $attributeSet);
if(isset($attributeSet['attribute_set_id'])) {
 
    /* Get attribute group information by using attribute set id and group name */
    $group_id = $config->getAttributeGroupId($attributeSet['attribute_set_id'], $attributeGroup);
    $attributeManagement->assign(
        'catalog_product',
        $attributeSet['attribute_set_id'],
        $group_id,
        $attributeCode,
        100
    );
}

If it helps then click on Kudos.

Problem solved? Click Accept as Solution!

Thank you

Hiren Patel