4

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;
    }
}
4
  • checkout_cart_save_before Method. Here is the lIst of events mageplaza.com/magento-2-module-development/… Please use Observer webkul.com/blog/how-to-create-custom-observers-in-magento2
    – Jackson
    Commented Dec 16, 2016 at 9:15
  • @Zinat Are you able to resolve this requirement? I am also facing a similar requirement. Need to customise the subtotal calculation. It would be helpful if you can post your answer.
    – Zammuuz
    Commented Apr 13, 2017 at 9:59
  • For changes in Subtotal u have to change in following tables quote, quote_item & quote_address
    – Jackson
    Commented Apr 25, 2017 at 8:34
  • You can change the price using custom price method like: $item->setCustomPrice($price); $item->setOriginalCustomPrice($price); $item->getProduct()->setIsSuperMode(true); $item->save(); and update all totals that you have: $quote->collectTotals() Commented Feb 23, 2021 at 8:59

2 Answers 2

1

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

0

I managed to changed the sub total for any item, here is the code:

$item->setCustomRowTotalPrice($item->getCustomPrice() * $item->getQty());
$item->setRowTotal($item->getCustomPrice() * $item->getQty());
$item->setBaseRowTotal($item->getCustomPrice() * $item->getQty());

This should be ok. This code couldn't work properly if you are running the function

calcRowTotal()

which calculate again the sub total for each item in the cart again overwriting your change.

Let me know if you have luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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