Magento – Quote item price changes

magento2quoteitem

Can someone suggest if there is any function to update the quote item tables price columns

(custom_price,original_custom_price,row_total,qty,price,base_price,row_total,base_row_total,price_incl_tax,base_price_incl_tax,row_total_incl_tax,base_row_total_incl_tax
etc)

changes internally rather than calculating and updating each column manually?

When a product price change happens, the quote details are not changing. I was trying to add code for the same while checking out, where mini cart details were not getting updated.

Best Answer

You can update cart item price using customer login observer. While customer login the cart item will updated with new price.

app/code/Vendor/Customer/etc/frontend/events.xml

<?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="customer_login">
        <observer name="vendor_customer_customer_login" instance="Vendor\Customer\Observer\CustomerloginObserver" />
    </event>
</config>

app/code/Vendor/Customer/Observer/CustomerloginObserver.php

namespace Vendor\Customer\Observer;

use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\Address;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Session;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote\Item;

/**
 * Class CustomerloginObserver
 * 
 */
class CustomerloginObserver implements ObserverInterface {

    /**
     * @var Session
     */
    protected $customerSession;

    /**
     * @var Customer
     */
    protected $customer;

    /**
     * @var Cart
     */
    protected $cart;

    /**
     * @var CheckoutSession
     */
    protected $checkoutSession;

    /**
     * @var Product
     */
    protected $_product;

    /**
     * @var Item
     */
    protected $quoteFactory;

    /**
     * @var CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * @var Address
     */
    protected $address;

    /**
     * CustomerloginObserver constructor.
     * @param Session $customerSession
     * @param Customer $customer
     * @param Cart $cart
     * @param CheckoutSession $checkoutSession
     * @param Product $_product
     * @param Item $quoteFactory
     * @param CartRepositoryInterface $quoteRepository
     */
    public function __construct(
        Session $customerSession,
        Customer $customer,
        Cart $cart,
        CheckoutSession $checkoutSession,
        Product $_product,
        Item $quoteFactory,
        CartRepositoryInterface $quoteRepository,
        Address $address
    ) {
        $this->customer = $customer;
        $this->customerSession = $customerSession;
        $this->cart = $cart;
        $this->address = $address;
        $this->checkoutSession = $checkoutSession;
        $this->_product = $_product;
        $this->quoteFactory = $quoteFactory;
        $this->quoteRepository = $quoteRepository;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {


        $id = $this->customerSession->getCustomerId();
        // check session is active or not.

        if ($session) {

            $itemId = 0;
            $qty = 0;
            $quoteId = 0;
            $sessionItems = $this->checkoutSession->getQuote()->getAllItems();

            foreach ($sessionItems as $session_item) {
                $qty = (int) $session_item['qty'];
                $productId = $session_item['product_id'];
                $product = $this->_product->load($productId);
                $setcustomprice = $this->checkoutSession->getQuote()->getItemById($session_item['item_id']);
                $uom = $product->getUom();

                    if ($price > 0) {
                        $customprice = $this->checkoutSession->getQuote()->getItemById($session_item['item_id']);
                        $customprice->setCustomPrice($price);
                        $customprice->setOriginalCustomPrice($price);
                        $customprice->save();
                        $quoteId = $session_item['quote_id'];
                    }
                }
            }

            if ($quoteId) {
                $quote = $this->quoteRepository->get($quoteId);
                $this->quoteRepository->save($quote->collectTotals());
            }
        }
}