How to add attribute option label and value in magento by coding?

How to add attribute option label and value in magento by coding?

September 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add attribute option label and value in magento by coding?

Adding, updating, and deleting Magento product attribute option values and labels through coding requires leveraging Magento’s built-in APIs and database operations. Here is a step-by-step guide on how to achieve this:

Part I: Adding an Attribute Option

To add an attribute option label and value programmatically in Magento, you can utilize the Magento\Eav\Model\Entity\AttributeRepository class.

Firstly, include the required namespace:

use Magento\Eav\Model\Entity\AttributeRepository;

Then, create a new function to add an attribute option:

public function addAttributeOption($attributeCode, $optionLabel, $optionValue)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();

    try {
        // Insert the new option into the 'eav_attribute_option' table
        $connection->query(
            "INSERT INTO eav_attribute_option (attribute_id, sort_order, value) VALUES (
                (SELECT entity_id FROM eav_attribute WHERE attribute_code = '" . $attributeCode . "'),
                0,
                '" . $optionValue . "'
            )"
        );

        // Insert the corresponding option label into the 'eav_attribute_option_label' table
        $connection->query(
            "INSERT INTO eav_attribute_option_label (option_id, store_id, value) VALUES (
                (SELECT MAX(entity_id) FROM eav_attribute_option),
                0,
                '" . $optionLabel . "'
            )"
        );

        return true;
    } catch (\Exception $e) {
        echo 'Error: ' . $e->getMessage();
        return false;
    }
}

In this function:

  • The $attributeCode parameter corresponds to the attribute code you want to add the option for.
  • The $optionLabel and $optionValue parameters are the desired option label and value, respectively.

Part II: Updating an Attribute Option

To update an existing attribute option, you can use the Magento\Eav\Model\Entity\AttributeRepository class again:

public function updateAttributeOption($optionId, $attributeCode, $newOptionLabel, $newOptionValue)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();

    try {
        // Update the option value
        $connection->query(
            "UPDATE eav_attribute_option SET value = '" . $newOptionValue . "'
            WHERE entity_id = " . $optionId
        );

        // Update the corresponding option label
        $connection->query(
            "UPDATE eav_attribute_option_label SET value = '" . $newOptionLabel . "' WHERE option_id = " . $optionId
        );

        return true;
    } catch (\Exception $e) {
        echo 'Error: ' . $e->getMessage();
        return false;
    }
}

In this function:

  • The $optionId parameter is the ID of the attribute option you want to update.
  • The $attributeCode, $newOptionLabel, and $newOptionValue parameters are used for validation purposes, but they are not required in the SQL queries.

Part III: Deleting an Attribute Option

To delete an attribute option programmatically:

public function deleteAttributeOption($optionId)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();

    try {
        // Delete the corresponding option label from the 'eav_attribute_option_label' table
        $connection->query(
            "DELETE FROM eav_attribute_option_label WHERE option_id = " . $optionId
        );

        // Delete the attribute option from the 'eav_attribute_option' table
        $connection->query(
            "DELETE FROM eav_attribute_option WHERE entity_id = " . $optionId
        );

        return true;
    } catch (\Exception $e) {
        echo 'Error: ' . $e->getMessage();
        return false;
    }
}

In this function:

  • The $optionId parameter is the ID of the attribute option you want to delete.

Conclusion

By leveraging Magento’s built-in classes and database operations, you can add, update, and delete product attribute options programmatically. This approach allows for more flexible and automated management of your product attributes without relying on the admin interface.

Remember to always validate user input and handle exceptions properly when working with the database, as shown in the provided examples.

For a comprehensive guide on Magento development best practices and advanced techniques, consider checking out LaravelCompany’s Magento Development Guide.

Catalog Database API Orders

Our network