Magento – Magento2, Customized quote subtotal in cart page

cartevent-observermagento2

Through observing the event checkout_cart_product_add_after, I was able to customize the price of the item in the cart, now I need to have an observer for customizing its subtotal calculation. What event/events need to be observed?

Is it possible to customize the calculation like $cart->setQuote()->getSubtotal() in observer and save the quote?

And basically, can it be done through observer or plugin is the solution?

Edit:

Here is my current event:

<event name="sales_quote_collect_totals_before">
        <observer name="customuitemprice" instance="vendor\code\Observer\BindUpdateItem"/>
    </event>

Observer:

class BindUpdateItem implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $quote = $observer->getQuote();
        foreach ($quote->getAllItems() as $item)
        {
            $product = $item->getProduct();
            ?How change the subtotal?

        }
        return $this;
    }
}

Best Answer

You can change the price using custom price method like:

$newPrice = '';
$item->setCustomPrice($newPrice);
$item->setOriginalCustomPrice($newPrice);
$item->getProduct()->setIsSuperMode(true);
$item->save();

and update all totals that you have:

$quote->collectTotals()

It is not a good idea to update only subTotals or grand totals because you will have a lot of problem with the final price calculation