Magento 2: Add a Custom Field to the Quote Table

Magento 2: Add a Custom Field to the Quote Table - Sb Dev Blog

The quote is an essential part of Magento 2. Magento’s cart is saved in the quote table. Therefore, we are often required to add some extra details to the cart.

We can save extra details in the cart at the item and quote levels. We can use custom options, additional options, and separate fields in the quote_item table at the item level.

Item-level information is helpful if we need to store different information per item. But if the information is constant throughout the quote, we should use the quote table instead of the quote_item table. In other words, the global values of the cart must be stored at the quote level for more significant maintenance of information and performance of the order journey.

We will create a custom field, sbdevblog_comment, in the quote table in this post. Also, we will save a sample comment in that field. We will use this field throughout checkout to save that field in order.

Magento 2: Add a Custom Field to the Quote Table

Let’s create a declarative schema file called db_schema.xml to create a field in the quote table.

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="quote" resource="default" engine="innodb">
        <column name="sbdevblog_comment" nullable="true" xsi:type="varchar" length="50" comment="SbDevBlog Comment"/>
    </table>
</schema>

Save values in the custom field of the quote table.

As we need to save value while adding a product to the cart, we will use the checkout_cart_product_add_after event.

So, let’s create an events.xml file. I have used frontend scope to save value at the front end of Magento 2. But we may use the desired scope according to the requirements.

<?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="checkout_cart_product_add_after">
        <observer name="sbdevcustomprice" instance="SbDevBlog\Checkout\Observer\SaveQuoteComment" />
    </event>
</config>

Finally, we must create an observer class defined in the above event.

<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */

namespace SbDevBlog\Checkout\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class SaveQuoteComment implements ObserverInterface
{
    private const QUOTE_COMMENT = "SB Dev Blog Comment";

    /**
     * ADD SAMPLE PRICE
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $quote = $observer->getEvent()->getData('quote_item')->getQuote();
        $quote->setData("sbdevblog_comment", self::QUOTE_COMMENT);
    }
}

That’s it; I hope you enjoyed SbDevBlog.

Thank you for reading the Magento 2: Add a Custom Field to the Quote Table post. Please subscribe and share with your connections. Use the comment section for your feedback. Thank you once again!

Click here to add custom prices while adding products to the cart.

Download the source code.

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

One thought on “Magento 2: Add a Custom Field to the Quote Table

Leave a Reply

Your email address will not be published. Required fields are marked *