eavSetupFactory = $eavSetupFactory; $this->attributeSetCollectionFactory = $attributeSetCollectionFactory; $this->attributeSetFactory = $attributeSetFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); // Create the attribute $eavSetup->addAttribute( Product::ENTITY, 'vendor', [ 'type' => 'int', 'label' => 'Vendor', 'input' => 'select', 'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class, 'required' => false, 'sort_order' => 50, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'group' => 'General', 'used_in_product_listing' => true, 'visible_on_front' => false, 'is_user_defined' => true, 'option' => [ 'values' => [ 'Thermo Fisher', 'Orion', 'Nalgene', 'Thermo Fisher Parts' ] ] ] ); // Assign the attribute to all existing attribute sets $attributeId = $eavSetup->getAttributeId(Product::ENTITY, 'vendor'); $entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY); $attributeSetCollection = $this->attributeSetCollectionFactory->create(); $attributeSetCollection->addFieldToFilter('entity_type_id', $entityTypeId); foreach ($attributeSetCollection as $attributeSet) { // Get the General group ID, or fallback to the first available group try { $groupId = $eavSetup->getAttributeGroupId( $entityTypeId, $attributeSet->getId(), 'General' ); } catch (\Exception $e) { // If General group doesn't exist, get the default group $groupId = $eavSetup->getDefaultAttributeGroupId( $entityTypeId, $attributeSet->getId() ); } // Add attribute to the set $eavSetup->addAttributeToGroup( $entityTypeId, $attributeSet->getId(), $groupId, $attributeId, null // Sort order ); } $setup->endSetup(); } }