Magento 2 - Set product attribute to use default values

Magento 2 - Set product attribute to use default values

July 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 - Set product attribute to use default values

Magento 2: Setting Product Attributes As Default Values Programmatically

You’re on the right track trying to set default product attributes programmatically in Magento 2. However, you encountered an issue where removing a product’s name attribute also removes it from categories.

Let me walk you through the correct approach to check ‘Use as Default’ for all products across a specific store view without accidentally deleting category assignments.

Understanding the Default Value Attribute

In Magento 2, each product attribute has a “Default Value” field. When this is checked, that particular value is used for every SKU within the context of your specified store view. It’s essentially a way to enforce a consistent naming convention or value across all products in that category.

Incorrect Approach: Deleting Attribute Data

The issue with your current approach lies in this line:

$product->setData('name', false);

When you set the data of an attribute (like ‘name’) to false, Magento interprets it as deleting the attribute entirely. This is why you’re seeing products being removed from their respective categories.

Correct Approach: Setting the Default Value Flag

To correctly set a product attribute as a default value, you need to update the default_value column in your database table. Here’s how:

  1. First, retrieve all SKUs for the given store view:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $this->productCollectionFactory->create();
$collection->addFieldToFilter('store_id', 1); // Replace with your desired store ID
  1. Loop through each product and set the default value flag:
foreach ($collection as $product) {
    $productId = $product->getEntityId();
    $connection = $objectManager->get('\Magento\Framework\App\ResourceConnection')->getConnection();
    $tableName = $objectManager->get('\Magento\Framework\App\ResourceConnection')->getTableName('catalog_product_entity_varchar');
    $connection->update(
        $tableName,
        ['default_value' => 1], // Set to 1 for true (checked)
        ['entity_id = ?' => $productId, 'attribute_id = ?' => 79] // Replace 79 with the ID of your attribute
    );
}

In this script:

  • I assume you’re working with a varchar type attribute. If it’s another data type like int, text, or decimal, you’ll need to use the appropriate table name and column name.
  • You can find the default value for an attribute by checking its ID in the Magento admin interface under Stores → Attributes.
  • The entity_id is the product ID, which we get using $product->getEntityId().
  • The attribute_id corresponds to the specific attribute you want to set as a default. You can find this by looking up your desired attribute’s ID in the database.

Additional Best Practices

While setting these values programmatically, keep some best practices in mind:

  1. Avoid using Object Manager: Directly using the object manager is not recommended for production code. It violates dependency injection principles and makes your code harder to test. Consider using proper service providers instead.

  2. Scope Your Changes: Make sure you’re setting the default values within a specific scope (store view or website). This prevents unintended global changes.

  3. Test Thoroughly: Before running this script on a live site, always test it in a development environment to ensure it behaves as expected.

Conclusion

Setting product attributes as default values programmatically is a powerful way to maintain a consistent naming convention across your Magento store. However, it’s crucial to understand the underlying database operations involved.

By following the approach outlined above and adhering to best practices, you can safely set default attribute values without accidentally deleting products from their categories.

If you’d like to learn more about advanced Magento 2 configuration options, be sure to check out our comprehensive guide at Laravel Company.

Happy coding!

Magento 2 Catalog Database Configuration

Our network