Magento 2 – Save Custom Value to Quote

custommagento2quote

I have added an extra column, by using custom module (upgradeScheme), into the table quote. How can I save my custom value into this column when quote is created? The value I want to add to this extra column can I get from the session. I just don't know how I can save this value into the quote. Please give a step by step instruction.

If I sum up my situation, it would be,

  1. I have added an extra column in the table quote.
  2. I can get the value I want to add to the custom column from session.
  3. I know how to convert/copy data from Quote to Order.
  4. I don't know how to save my custom value to the extra column I created in Quote.

Best Answer

You can Use Event checkout_cart_product_add_after.

in your frontend/events.xml

<event name="checkout_cart_product_add_after">
        <observer name="after_add_to_cart" instance="your\module\Observer\AddToCartAfter" />
</event>

in observer file AddToCartAfter.php

public function execute(\Magento\Framework\Event\Observer $observer) {

    $quoteItem = ( $quoteItem->getParentItem() ? $quoteItem->getParentItem() : $quoteItem );

    $quoteItem->setCustomField("Custom Value");
    //here u set value of your field of quote

}
Related Topic