Fix to make vendor attribute user-defined, add to Default attribute set

This commit is contained in:
shopkeeperdev
2025-10-08 18:18:37 -04:00
parent 6a75fa3a5d
commit 179979342e

View File

@@ -9,6 +9,7 @@ use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\Product; use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory; use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
use Magento\Catalog\Model\Product\Attribute\Backend\Sku;
class InstallData implements InstallDataInterface class InstallData implements InstallDataInterface
{ {
@@ -46,6 +47,7 @@ class InstallData implements InstallDataInterface
'group' => 'General', 'group' => 'General',
'used_in_product_listing' => true, 'used_in_product_listing' => true,
'visible_on_front' => false, 'visible_on_front' => false,
'is_user_defined' => true,
'option' => [ 'option' => [
'values' => [ 'values' => [
'Thermo Fisher', 'Thermo Fisher',
@@ -65,10 +67,31 @@ class InstallData implements InstallDataInterface
$attributeSetCollection->addFieldToFilter('entity_type_id', $entityTypeId); $attributeSetCollection->addFieldToFilter('entity_type_id', $entityTypeId);
foreach ($attributeSetCollection as $attributeSet) { foreach ($attributeSetCollection as $attributeSet) {
$groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSet->getId(), 'General'); // Get the General group ID, or fallback to the first available group
$eavSetup->addAttributeToGroup($entityTypeId, $attributeSet->getId(), $groupId, $attributeId, null); 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(); $setup->endSetup();
} }
} }