Magento 2 How to set custom data / option in a quote item?

Magento 2 How to set custom data / option in a quote item?

September 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 How to set custom data / option in a quote item?

Persisting Custom Data in Magento 2 Quote Items

As you’re exploring Magento 2’s quote management, setting custom data for quote items can be quite useful for tracking additional information or controlling behavior across multiple requests. In your current approach, you’ve successfully set the customname and isSuperMode properties on quote items within a single request scope.

However, as you correctly observed, this data is not persisted beyond the immediate request lifecycle due to Magento’s object caching and serialization processes. To make your custom data accessible across subsequent requests, we need to leverage Magento’s persistence mechanisms more effectively.

Storing Custom Data Properly

To ensure your custom data survives across requests, you should store it in a way that Magento can retrieve it from the database or cache for each quote item. One common approach is to use the quote_item table directly and add new columns for your custom fields.

For example, if you want to add a ‘custom_field’ to all quote items:

  1. Open your database management tool (e.g., MySQL Workbench).

  2. Locate the quote_item table in your Magento 2 database.

  3. Add a new column named custom_field. Let’s use a text field for simplicity:

ALTER TABLE `quote_item`
ADD COLUMN `custom_field` TEXT NOT NULL;
  1. Now, you can set and get this value directly from the quote item object:
// Set custom data
$eachQuoteItem->setCustomField('aaaa');
$eachQuoteItem->save();

// Get custom data in another request
$quoteItems = $this->cart->getItems();
foreach ($quoteItems as $eachQuoteItem) {
    echo $eachQuoteItem->getCustomField(); // Outputs: aaaa
}

This approach ensures that your custom field is stored in the database and survives across requests. However, it requires direct table manipulation and may not be ideal for more complex data structures or business logic.

A More OOP Approach

For a cleaner, object-oriented solution, consider creating an extension attribute for the quote_item entity. Extension attributes allow you to add new fields without directly modifying core tables:

  1. Create a new module in your Magento installation (e.g., Company_CustomQuote).

  2. In the module’s etc/extension_attributes.xml, define your custom field:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
        <attribute code="custom_field" type="string"/>
    </extension_attributes>
</config>
  1. Now, you can set and get the custom field using Magento’s standard object management:
// Set custom data
$eachQuoteItem = $this->cart->getItems()[0];
$eachQuoteItem->setExtensionAttributes([
    'custom_field' => 'aaaa',
]);
$eachQuoteItem->save();

// Get custom data in another request
$quoteItems = $this->cart->getItems();
foreach ($quoteItems as $eachQuoteItem) {
    echo $eachQuoteItem->getExtensionAttributes()->getCustomField(); // Outputs: aaaa
}

This approach keeps your data model clean and leverages Magento’s native extension attributes feature, making it easier to maintain and upgrade.

Conclusion

To persist custom data in Magento 2 quote items across requests, you have two primary options:

  1. Directly alter the quote_item database table to add new columns for your custom fields.
  2. Create an extension attribute for the quote_item entity using your own module.

Both methods allow you to store and retrieve custom information associated with each quote item in a way that survives across multiple requests. The choice between them depends on your specific requirements, data complexity, and maintenance preferences.

Remember to thoroughly test any changes to ensure they don’t interfere with Magento’s core functionality or other extensions. Always consult the official Magento 2 documentation for the latest best practices and guidelines.

I hope this explanation helps clarify how to work with custom data in Magento 2 quote items effectively. If you have any further questions, feel free to explore our other Magento tutorials at LaravelCompany.

Magento 2 Cache Database Configuration

Our network