3

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.

4

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());
            }
        }
}
3

First create a module using free module creator tool called silk software

Magento 2 Module Creator Tool Using Silk Software

and enable using following commands below.

 1. php bin/magento module:enable Tw_Cart
 2. php bin/magento setup:upgrade
 3. php bin/magento setup:di:compile
 4. php bin/magento setup:static-content:deploy

create events.xml in the path:- app/code/Tw/Cart/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="checkout_cart_product_add_after">
        <observer name="Tw_Cart_Addtocart_Customprice" instance="TW\Cart\Observer\CustomPrice" />
    </event>
</config>

create CustomPrice.php in the path: app/code/Tw/Cart/Observer/ & keep the below code

<?php
    namespace TW\Cart\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
    use Psr\Log\LoggerInterface;
    class CustomPrice implements ObserverInterface
    {

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
            $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/stackexchange.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            /* Code here */
            $quote_item = $observer->getEvent()->getQuoteItem();
            $price = 400; //set your price here
            $quote_item->setCustomPrice($price);
            $quote_item->setOriginalCustomPrice($price);
            $quote_item->getProduct()->setIsSuperMode(true);
            $logger->info("success !!!!");
    }

}
  • 2
    2 weeks of head scratching as to why my custom prices weren't carrying over in to the totals sorted! Thanks :) – sulman Sep 19 '17 at 12:50
  • I implemented above code. It's working only first time added to cart when cart is empty. second time it's taking original value of product. – Sunny Khatri May 18 '18 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.