Update product attribute value programmatically via observer

In Magento 2, you might need to update product attribute values programmatically instead of manually editing them in the admin panel. This can be done efficiently using the class Magento\Catalog\Model\Product\Action inside an observer, which allows you to react to specific events and update product attributes automatically.

This approach is especially useful for:

  • Updating custom attributes like YouTube links, tags, or SKU automatically.
  • Bulk-updating product attributes when certain events occur.
  • Integrating with external systems or APIs.

Using Magento\Catalog\Model\Product\Action

The Product\Action class provides the updateAttributes() method, which allows you to update one or multiple product attributes in bulk. This method is more efficient than loading and saving each product individually.

Example: Update a Custom Product Attribute via Observer

Here’s a working example of an observer that sets the value of a custom product attribute snippet_youtube_link to null whenever the observer is triggered:


namespace PL\Snippets\Observer;

use Magento\Catalog\Model\Product;

use Magento\Framework\Event\Observer;

class UpdateProductAttribute implements \Magento\Framework\Event\ObserverInterface

{

    const SNIPPET_YOUTUBE_LINK = 'snippet_youtube_link';

    protected $productAction;

    protected $storeManager;

    public function __construct(

        \Magento\Catalog\Model\Product\Action $productAction,

        \Magento\Store\Model\StoreManagerInterface $storeManager

    ) {

        $this->productAction = $productAction;

        $this->storeManager = $storeManager;

    }

    public function execute(Observer $observer)

    {

        $productIds = $observer->getEvent()->getData('productIds');

        if(is_array($productIds) && count($productIds) > 0) {

            $storeId = $this->storeManager->getStore()->getId();

            $this->productAction->updateAttributes($productIds, array(self::SNIPPET_YOUTUBE_LINK => null), $storeId);

        }

    }

}

How It Works

  1. Dependency Injection: The observer injects the Product\Action class and StoreManagerInterface to handle product updates and determine the store ID.
  2. Fetching Product IDs: The observer listens to an event and retrieves the affected product IDs.
  3. Bulk Update: The updateAttributes() method updates the custom attribute for all products in the $productIds array at once.
  4. Store Scope: The update is applied to the current store, ensuring store-specific values are correctly managed.

Updating product attributes programmatically in Magento 2 is straightforward with Magento\Catalog\Model\Product\Action and an observer. This method allows bulk updates, automation, and efficient handling of product data without manually editing each product.

By following the example above, you can update any product attribute, such as custom YouTube links, dynamically whenever an event occurs.

Back to Top