-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Closed
Labels
Issue: ready for confirmationReported on 2.4.6Indicates original Magento version for the Issue report.Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itIssue related to Developer Experience and needs help with Triage to Confirm or Reject it
Description
In our store that we have we have multiple tax_class_id attributes.
It was a migration from magento 1 to magento 2.3.4 afterwards it was upgraded to 2.4.4 and afterwards to 2.4.6

can we delete those extra tax_class_id attributes ?
14 is from quote_address_item
15 is from quote_item
The question i'm asking this is that we somehow missing the tax class on product create/edit page.
we have created script to delete attribute and create it again.
and as far as i know there should be only one tax_class_id attribute ( other shops have only 1 )
here is the script i wrote to do that:
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('adminhtml');
$eavSetupFactory = $objectManager->get(\Magento\Eav\Setup\EavSetupFactory::class);
$eavConfig = $objectManager->get(\Magento\Eav\Model\Config::class);
$resource = $objectManager->get(\Magento\Framework\App\ResourceConnection::class);
$db = $resource->getConnection();
$entityTypeCode = 'catalog_product';
$attributeCode = 'tax_class_id';
$defaultAttributeGroup = 'Product Details';
// Load entity type
$entityTypeFactory = $objectManager->get(\Magento\Eav\Model\Entity\TypeFactory::class);
$entityTypeModel = $entityTypeFactory->create()->loadByCode($entityTypeCode);
$entityTypeId = $entityTypeModel->getId();
$attributeSetId = $entityTypeModel->getDefaultAttributeSetId();
// Load attribute group ID
$groupCollectionFactory = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory::class);
$groupCollection = $groupCollectionFactory->create()
->setAttributeSetFilter($attributeSetId)
->addFieldToFilter('attribute_group_name', $defaultAttributeGroup);
$attributeGroupId = $groupCollection->getFirstItem()->getId();
if (!$attributeGroupId) {
echo "❗ Cannot find attribute group '$defaultAttributeGroup'. Exiting.\n";
exit(1);
}
echo "✅ Attribute Set ID: $attributeSetId\n";
echo "✅ Attribute Group ID: $attributeGroupId\n";
// Check for existing attribute
$oldAttribute = $db->fetchRow("
SELECT attribute_id FROM eav_attribute
WHERE attribute_code = ? AND entity_type_id = ?
", [$attributeCode, $entityTypeId]);
$preservedValues = [];
if ($oldAttribute) {
$oldAttributeId = (int) $oldAttribute['attribute_id'];
echo "📦 Backing up existing tax_class_id values for attribute ID $oldAttributeId...\n";
$preservedValues = $db->fetchAll("
SELECT store_id, entity_id, value
FROM catalog_product_entity_int
WHERE attribute_id = ?
", [$oldAttributeId]);
echo "✅ Found " . count($preservedValues) . " values to preserve.\n";
// Delete old attribute
echo "🧹 Deleting old attribute ID $oldAttributeId...\n";
$db->delete('catalog_eav_attribute', ['attribute_id = ?' => $oldAttributeId]);
$db->delete('eav_entity_attribute', ['attribute_id = ?' => $oldAttributeId]);
$db->delete('eav_attribute', ['attribute_id = ?' => $oldAttributeId]);
$db->delete('catalog_product_entity_int', ['attribute_id = ?' => $oldAttributeId]);
}
// Clear EAV config cache
$eavConfig->clear();
// Add attribute again
echo "➕ Adding new tax_class_id attribute...\n";
$eavSetup = $eavSetupFactory->create(['setup' => $objectManager->get(\Magento\Framework\Setup\ModuleDataSetupInterface::class)]);
$eavSetup->addAttribute($entityTypeCode, $attributeCode, [
'type' => 'int',
'label' => 'Tax Class',
'input' => 'select',
'source' => \Magento\Tax\Model\TaxClass\Source\Product::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE,
'required' => false,
'user_defined' => false,
'default' => '2',
'visible' => true,
'used_in_product_listing' => true,
'visible_on_front' => false,
'sort_order' => 80,
'group' => $defaultAttributeGroup,
]);
$newAttributeId = $eavSetup->getAttributeId($entityTypeCode, $attributeCode);
echo "✅ New attribute created with ID $newAttributeId\n";
// Re-assign to set/group
$attribute = $eavConfig->getAttribute($entityTypeCode, $attributeCode);
$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);
$attribute->save();
// Restore preserved values
if (!empty($preservedValues)) {
echo "🔁 Restoring " . count($preservedValues) . " values to new attribute ID $newAttributeId...\n";
foreach ($preservedValues as $row) {
$db->insert('catalog_product_entity_int', [
'attribute_id' => $newAttributeId,
'store_id' => $row['store_id'],
'entity_id' => $row['entity_id'],
'value' => $row['value'],
]);
}
echo "✅ Values restored.\n";
} else {
echo "⚠️ No values to restore.\n";
}
echo "🎉 Done.\n";
Metadata
Metadata
Assignees
Labels
Issue: ready for confirmationReported on 2.4.6Indicates original Magento version for the Issue report.Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itIssue related to Developer Experience and needs help with Triage to Confirm or Reject it