Magento2 - Add Custom Column in cart item table and insert data to custom column when product add to cart

i have added new column in 'quote_item' table. i want to add the data to custom column when user add any product to cart.

Solutions

You can use event/observer to achieve this.

Use following codes in your module.

etc/frontend/events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
        <observer name="bialcheckout_product_add_to_cart" instance="Vendor\Module\Observer\ProductAddToCart" />
    </event>
</config>

Observer/ProductAddToCart.php

<?php
namespace Vendor\Module\Observer;

use Magento\Checkout\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteRepository;

class ProductAddToCart implements ObserverInterface
{
    public function __construct(
        Session $session,
        QuoteRepository $quoteRepository
    )
    {
        $this->session = $session;
        $this->quoteRepository = $quoteRepository;
    }

    public function execute(Observer $observer)
    {
        $quoteId = $this->session->getQuote()->getId();
        if ($quoteId) {
            $quote = $this->quoteRepository->get($quoteId);
            if (!$quote->getIsActive()) {
                return;
            }

        $product = $observer->getEvent()->getDataByKey('product');
        /** @var \Magento\Quote\Model\Quote\Item $item */
        $item = $this->session->getQuote()->getItemByProduct($product);
        $itemId = $item->getId();
        $quoteItem = $quote->getItemById($itemId);
        $customValue = 10;  // Prepare your custom field value here
        $quoteItem->setCustomField($customValue); // Set custom field value
        $quoteItem->save();
        }
        return $this;
    }
}

Similar questions

new product type image of bundled product - children product price not insert in table sale_flat_quote_item and sale_flat_order_item
I created a new product type image of bundled product and everything is work except the children product cannot insert the price in to database table salt_flat_quote_item and sale_flat_order_item. This turned out to give some problems in the order in the backend cannot show children price of this product. Do i have to call a function to get my chil...
Magento order with parent item and child item instead of only child item
i recently discovered a issue with some of our orders. We have configuable products with simple products as childs, selectable via one attribute. Example: The sizes are the attribute to select in the configurable product. Until last week, when a customer chosed a child product, let's say #972, the order information in the backend showed this child ...
Magento 2 Add to cart api Item price set zero in Quote Item table
I am trying to add product to cart with Magento 2 default API. but when i first time add product to cart using API than it set product price 0 in Quote Item table. After second time add product to cart using same API than it working fine. I have try with Fresh Magento 2 but facing same issue.
Magento 2: How to insert data into custom table in model file?
Magento version: 2.0 I created a table to store some data. but maybe I have to use many file to do it from some tutorials, such as model, resource and collection folder's files. I just save, update or delete the data into table, don't need to many works with it. So, there is simple method to do it?
Magento 2 Insert Data into custom table using Collection Factory
I am following tutorials at http://alanstorm.com/magento_2_crud_models_for_database_access/ for creating my extension but theres one key part of that tutorial that appears to be out of date. This Part : SetData and Save no longer work this way. So how can I use a Collection Factory to create a new item in my table?
How to insert/update data in custom table using Magento 2 using Ajax but 404 error occur on ajax hit?
I have created a custom module for admin area in which i have show some data in phtml file. I have created a custom table in database using installschema.php. Now i want to save that data in database but on hitting the update button ajax url is hit but the response it gives me 404. My file path are My URL is http://127.0.0.1/magento/AdminSample/dat...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.