How can I custom options to an item dynamically when it is added to the cart in Magento?

How can I custom options to an item dynamically when it is added to the cart in Magento?

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I custom options to an item dynamically when it is added to the cart in Magento?

Customizing Item Options Dynamically in Magento: A Comprehensive Guide

As an experienced senior developer at Laravel Company, I understand the challenges that arise when trying to customize item options dynamically in Magento, especially for products with multiple logo and thread color combinations. In this comprehensive guide, we will explore a practical solution to your problem, step by step.

Firstly, let’s break down your current implementation and identify the key areas of improvement:

  1. You have correctly set up an observer that triggers when a product is added to the cart (checkout_cart_product_add_after event).
  2. The observer successfully retrieves custom options for the selected product.
  3. It identifies the thread color option and prepares a replacement option with a generic label (“Thread Color”).
  4. However, the critical issue lies in the database querying process when you attempt to remove the original custom option from the cart item.

The SQL error you encountered is caused by an incorrect serialization of the additional_options value in your code. Let’s take a closer look at the problematic section:

$item->addOption(array(
    'code' => 'additional_options',
    'value' => serialize($additionalOptions),
));

In this line, you are serializing the $additionalOptions array before adding it as an option to the cart item. However, when retrieving and manipulating these options later in your code, you are not properly decoding them back into an associative array.

To resolve this issue, we need to make a few strategic modifications:

  1. Modify the observer method checkoutCartProductAddAfter:
public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
    // Your existing code...

    if ($additionalOptions = $item->getOptionByCode('additional_options')) {
        $additionalOptions = unserialize($additionalOptions->getValue());

        foreach ($additionalOptions as &$option) {
            if (preg_match('/select thread color/', strtolower($option['label'])) &&
                strtolower($option['value']) != 'no thread') {

                // Your existing code to create replacement option...

                // Update the additional options array
                $key = array_search($optId, array_column($additionalOptions, 'option_id'));
                unset($additionalOptions[$key]);

                // Add replacement additional option for reorder (see above)
                if (!isset($infoArr['additional_options'])) {
                    $infoArr['additional_options'] = [];
                }
                $infoArr['additional_options'][] = $additionalOption;
            }
        }

        // Update the additional options option with the modified array
        $item->setData('option_id', $additionalOptions);
    }

    // Your existing code...
}
  1. Modify the observer method salesConvertQuoteItemToOrderItem:
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
        $options = unserialize($additionalOptions->getValue());

        // Filter out the thread color options
        $filteredOptions = array_filter($options, function ($option) {
            return strtolower($option['value']) == 'no thread' || preg_match('/select thread color/', strtolower($option['label'])) && strtolower($option['value']) != 'no thread';
        });

        // Remove the filtered options from the additional_options
        $quoteItem->setData('additional_options', serialize(array_diff($options, $filteredOptions)));
    }
}

With these modifications, we are now properly decoding and manipulating the serialized additional_options array before processing it further in your code. This ensures that the correct data is being passed between the observer methods and avoids the SQL error you encountered earlier.

Additionally, by using the array_filter() function with a custom callback, we can efficiently filter out the unwanted thread color options from the additional options array when converting the quote item to an order item.

Remember to clear your Magento cache after making these changes to ensure that the updates take effect immediately.

I hope this comprehensive guide helps you resolve your issue and provides a clear path forward for dynamic customization of item options in Magento. If you have any further questions or need assistance with other Magento-related challenges, feel free to reach out to us at Laravel Company.

Happy coding!

Checkout Catalog Database Extensions

Our network