Get product Custom option value in event sales_quote_add_item

Get product Custom option value in event sales_quote_add_item

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get product Custom option value in event sales_quote_add_item

Customizing Product Prices Based on Options in Magento

Hello! I’m glad you’re exploring customizations within your Magento store to adjust product prices based on selected options during the cart process. In this post, let’s dive into how you can achieve this using an event observer and the sales_quote_add_item event.

Understanding the Current Scenario

You’ve already set up the event configuration for sales_quote_add_item, which hooks your observer to modify prices when an item is added to a shopping cart. However, while you’re successfully retrieving custom options, you’re struggling to extract the actual option values and apply them to price adjustments.

Accessing Custom Option Values

The key to solving this lies in properly accessing and iterating through the custom_options data associated with the quote item. Let me guide you through the process:

public function execute(\Magento\Framework\Event\Observer $observer) {
    $item = $observer->getEvent()->getData('quote_item');
    $options = $item->getProductOptions();

    if (isset($options['options']) && !empty($options['options'])) {
        foreach ($options['options'] as $option) {
            // Access option values
            $optionValue = $option['option_value'];
            $title = $option['label'];

            // Your logic to calculate price based on options goes here
            $priceAdjustment = 0; // Example: Add $5 for every selected option

            if ($optionValue != '') {
                $priceAdjustment += 10; // Example: Add another $10 for non-empty values
            }

            // Update the item price
            $item->setCustomPrice($item->getProduct()->getPrice() + $priceAdjustment);
            $item->setOriginalCustomPrice($item->getProduct()->getPrice() + $priceAdjustment);
        }
    }

    // Set super mode to true if custom pricing is applied
    $item->getProduct()->setIsSuperMode(true);
}

Calculating the Price Adjustment

Within the observer’s execute method, we iterate through each option associated with the item. Here, you can add your specific business logic to calculate price adjustments based on the selected options.

For instance, if you want to:

  • Add a fixed amount of $5 for every selected option: $priceAdjustment += 5;
  • Add an additional $10 if the option value is not empty: if ($optionValue != '') { $priceAdjustment += 10; }

Setting Custom Prices

After calculating the price adjustment, we update the item’s custom and original custom prices using the existing product price. We also set is_super_mode to true for the associated product to enable super mode pricing.

// Update the item price
$item->setCustomPrice($item->getProduct()->getPrice() + $priceAdjustment);
$item->setOriginalCustomPrice($item->getProduct()->getPrice() + $priceAdjustment);

// Set super mode to true if custom pricing is applied
$item->getProduct()->setIsSuperMode(true);

Conclusion

By implementing this observer and adjusting the logic within the execute method, you should now be able to customize product prices based on selected options in your Magento store.

Remember to clear your cache after making changes to ensure the updates take effect. If you need further assistance or want to explore other customizations like adding taxes or discounts based on options, feel free to check out our comprehensive guides at Laravel Company.

Happy coding, and don’t hesitate to reach out if you have any questions!

Checkout Catalog Extensions Configuration

Our network