Update Product Name Magento 2

Update Product Name Magento 2

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Update Product Name Magento 2

Updating Magento 2 Product Attributes: A Comprehensive Guide

Updating product attributes in Magento 2 can be a complex task for beginners, but with the right approach, it becomes quite straightforward. Let’s dive into this process, addressing your specific issue of updating product names.

Understanding the Current Approach

You’ve already attempted to update the product name using Magento’s native Object Manager. This method involves injecting the Magento\Catalog\Model\ProductFactory and loading a specific product by its SKU. Here’s what you’ve done so far:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productFactory = $objectManager->create('\Magento\Catalog\Model\ProductFactory');
$product = $productFactory->create();
$product->load($product->getIdBySku($sku));
if(!empty($product->getData('sku')))
{
    $product->setName('Test1234');
    $product->save();
}

While this approach can work in some cases, it has several limitations:

  • It’s prone to errors if the object manager is not properly set up.
  • It doesn’t handle the product status or visibility correctly.
  • It bypasses Magento’s event system and may cause unexpected behavior.

The Correct Way: Using Plugins

To update a product name safely and efficiently, we recommend using Magento’s plugin system. This approach ensures that your code adheres to Magento’s architecture and follows best practices.

Here’s how you can create a plugin to update the product name:

  1. Create a new class in your custom module, extending \Magento\Catalog\Model\Product:
use Magento\Catalog\Model\Product;

class UpdateProductName extends Product
{
    // Your code here
}
  1. Define the beforeSave method and update the name there:
public function beforeSave()
{
    if ($this->hasChanged('sku') || $this->isObjectNew()) {
        $this->setName('Updated Name');
    }
    return parent::beforeSave();
}
  1. In your di.xml, declare the plugin:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="update-product-name-before-save"
                type="YourVendor\YourModule\Model\Product\UpdateProductName"/>
    </type>
</config>

Note that you should replace YourVendor and YourModule with your actual vendor name and module name.

When to Use Each Approach

The Object Manager approach can be used in a very limited context, such as in cron jobs or background processes where the state of the object manager is guaranteed. However, for most development tasks, including updating product attributes, using Magento’s built-in plugins is recommended.

Laravel Company provides detailed guides on Magento 2 plugin usage and best practices. I recommend checking out their Magento 2 Plugin Tutorial to learn more about this topic.

Conclusion

Updating product attributes in Magento 2 requires a deep understanding of the system’s architecture. The Object Manager approach should be avoided for most tasks due to its potential risks and limitations.

By using Magento’s plugin system, you ensure that your code is integrated with the platform correctly and follows industry best practices. Always remember to declare your plugins in di.xml and use them wisely.

Remember, the key is to extend Magento’s built-in models and leverage its events for any customization needs. This approach ensures a stable and maintainable system over time.

If you have any further questions or need additional guidance on Magento 2 development, feel free to reach out to our team at Laravel Company.

Magento 2 Catalog Extensions Errors

Our network