Magento - change product custom options values in observer

Magento - change product custom options values in observer

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - change product custom options values in observer

Changing Product Custom Options Values During Cart Addition: A Magento Observer Approach

In Magento, custom product options play a crucial role in enhancing customer experience by allowing personalized configurations. However, modifying these options programmatically during the cart addition process requires a strategic approach using observers to intercept and manipulate data at the right stage of the checkout process.

The provided pseudo code hints at the correct direction but lacks the necessary context for a functional implementation.

Understanding Magento Observers

Magento observers are event-driven listeners that allow you to hook into specific actions within the application flow, such as order creation or product addition. They consist of an observer class and an XML configuration file that defines which events they should listen to.

For our use case, we’re interested in the sales_quote_item_set_product event, which is triggered when a product is added to a quote (shopping cart) via the checkout process.

Implementing the Observer

First, let’s create an observer class. In your custom module folder (app/code/YourCompany/YourModule/Observer), create an UpdateProductOptions.php file:

namespace YourCompany\YourModule\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Quote\Item;

class UpdateProductOptions implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Get the quote item from the observer data
        $quoteItem = $observer->getQuoteItem();

        // Check if we have custom options attached to this product
        if ($quoteItem->hasOptions()) {
            $options = $quoteItem->getOptions();
            foreach ($options as $option) {
                // For each option, let's set a new value
                if ($option->getOptionId() == YOUR_CUSTOM_OPTION_ID) {
                    $optionValue = 'My new value';
                    $option->setValue($optionValue);
                }
            }
        }

        return $this;
    }
}

Configuring the Observer

Next, update your module’s etc/events.xml file to include event configuration:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_item_set_product">
        <observer name="yourcompany_yourmodule_update_product_options" instance="YourCompany\YourModule\Observer\UpdateProductOptions"/>
    </event>
</config>

Ensure that this file is located in your module’s etc folder and the observer class name matches the instance attribute.

Deploying the Observer

After implementing the observer and updating the configuration, deploy your custom module:

  1. Run bin/magento setup:upgrade from the Magento root directory.
  2. Clear the cache with bin/magento cache:flush.

Now, whenever a product with the specified custom option is added to the cart, the observer will intercept it and update the option value according to your specifications.

Fine-tuning the Solution

Remember that this is a basic example. In a real-world scenario, you would need to ensure proper validation of the option values, handle potential exceptions gracefully, and potentially split the logic into separate services for better code organization.

For more advanced topics on Magento observers and custom options manipulation, consider exploring our comprehensive guide at LaravelCompany.

Conclusion

Changing product custom option values during the cart addition process in Magento requires a strategic approach using event-driven programming. By implementing an observer for the sales_quote_item_set_product event and updating the custom options within this observer, you can effectively modify option values as needed.

This technique allows you to maintain the integrity of your checkout process while providing enhanced flexibility in managing product configurations. Always ensure that your changes are logically validated and do not disrupt the core functionality of the platform.

Don’t hesitate to reach out if you have any questions or need further assistance with Magento customizations. Happy coding!

Checkout Catalog API Extensions

Our network