Get product order custom attributes in save_order_save_after observer in Magento 2

Get product order custom attributes in save_order_save_after observer in Magento 2

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get product order custom attributes in save_order_save_after observer in Magento 2

Capturing Product Custom Attributes in Magento 2 Order Observer

When you want to capture custom product attributes during an order save event in Magento 2, there are several ways to approach this task. In this post, we’ll provide a step-by-step guide on how to achieve this using observers.

Understanding the Save Order Event in Magento 2

Magento 2 uses the save_order_after event to trigger actions after an order is saved or updated. This is the ideal place to capture product information and custom attributes associated with the order’s items.

To start, navigate to your app/code directory:

cd /path/to/your/magento_root/app/code

Creating a New Observer

Let’s create a new observer module called Company_ProductAttributeCapture. Follow these steps:

  1. Create the module structure:
mkdir -p Company/ProductAttributeCapture/etc/events
  1. Create the di.xml configuration file in the etc directory to define your observer:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="save_order_after">
        <observer name="company_product_attribute_capture_observer" instance="Company\ProductAttributeCapture\Observer\SaveOrderAfter"/>
    </event>
</config>
  1. Now, let’s create the observer class Observer in the etc/events directory:
namespace Company\ProductAttributeCapture\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;

class SaveOrderAfter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        // Loop through order items to capture custom product attributes
        foreach ($order->getAllVisibleItems() as $item) {
            $product = $item->getProduct();
            $customAttributeValue = $product->getData('your_custom_attribute_code');

            // You can process or save this value here as needed
            // For example, you might want to add it to a log file or send it via email

            Magento_Backend_Model_Session::addSuccess("Custom attribute captured: {$customAttributeValue}");
        }

        return $this;
    }
}

Replace your_custom_attribute_code with the actual code of the custom product attribute you want to capture.

Installing and Enabling Your Observer Module

To install your new module, run the following commands:

php bin/magento setup:upgrade
php bin/magento cache:clean

Capturing Custom Attributes in Magento 2

In this observer, we’re using the getData() method to retrieve custom attribute values directly from the product object. This is a simple and effective way to access these attributes within an event listener.

Remember that you should always check if the product or item has the desired attribute before trying to retrieve its value to avoid any potential errors or exceptions.

Extending Your Observer Capabilities

If you need more sophisticated processing of custom attributes, consider using Magento’s getAttribute() and getAttributeText(‘attribute_code’) methods. These allow for advanced filtering and data manipulation:

$customAttributeValue = $product->getAttribute('your_custom_attribute_code');
$formattedAttributeValue = $product->getAttributeText('your_custom_attribute_code');

Best Practices for Magento 2 Observers

When working with observers, keep the following best practices in mind:

  • Keep your observer logic concise and focused on a single task.
  • Avoid heavy computations or database queries within the observer to ensure optimal performance.
  • Use Magento’s logging mechanisms (Magento\Backend\Model\Session in this example) sparingly to avoid flooding the system with unnecessary messages.

Conclusion

In this post, we demonstrated how to capture custom product attributes during an order save event using a Magento 2 observer. By following these steps and understanding the event lifecycle, you can easily extend Magento’s core functionality to suit your specific business needs.

Remember that well-structured observers are key to maintaining a clean and maintainable codebase in any Magento project. If you found this guide helpful, visit LaravelCompany.com for more Magento 2 tutorials and custom development services.

Magento 2 Catalog Extensions Configuration

Our network