Magento 2 Custom Product Price Calculation and Overriding Cart Totals

by Andy T   Last Updated July 12, 2019 14:09 PM - source

Background:

I am working on a project with very custom price calculations for specific transfer products. These transfer products have tiered pricing applied to them, however, the first product purchased is always charged at £13.17, subsequent products are then charged at the tiered product rate for the quantity being purchased

e.g. When a customer purchases 10 bespoke transfers 1 should be charged at £13.17 and 9 should be charged at £7.75.

I have achieved this using pricing using the checkout_cart_product_add_after and checkout_cart_update_items_after events. However, the custom price set for the products in the observer, more often than not comes to 3 or 4 decimal places. Using the example above the average price for each product is £8.2920. Magento then rounds this to the nearest 2 decimal places (£8.29) making the order total £82.90, instead of £82.92. The higher the order quantity, the further out the two figures are.

Current Approach

Created an observer for the sales_quote_address_collect_totals_after event, where I am using the setRowTotal and setBaseRowTotal functions to correct the total value for each transfer item in the cart.

The Problem

I am modifying the the $total's object within the observer which is temporarily changing the subtotal value in the summary box, however, the spinning wheel appears and reverts the subtotal value back to the original value.

Below a screenshot of the cart with a corrected $item rowTotal value but incorrect subtotal.

enter image description here

How do I override the cart totals without them being reverted back? I also need the tax amount and grand total to reflect the updated totals for each item in the cart.

Below is the execute function of my sales_quote_address_collect_totals_after observer

public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $quote = $observer->getData('quote');
        $shippingAssignment = $observer->getData('shipping_assignment');
        $total = $observer->getData('total');


        if ($quote->hasItems()) {

            $customTotal = 0;
            foreach($quote->getItems() as $item) {
                $customTotal = $customTotal + ($item->getCustomPrice() * $item->getQty());
                // override the row totals for each product using the custom price
                $item->setRowTotal($item->getCustomPrice() * $item->getQty());
                $item->setBaseRowTotal($item->getCustomPrice() * $item->getQty());
            }

        }

        // override totals price (not sure on effect with tax)
        $total->setTotalAmount('subtotal', $customTotal);
        $total->setBaseTotalAmount('subtotal', $customTotal);
        $total->setSubtotalWithDiscount($customTotal);
        $total->setBaseSubtotalWithDiscount($customTotal);
        $total->setBaseWithDiscount($customTotal);

        return $this;
    }

Thanks in advance for any help with this issue.



Related Questions


Showing tier price on cart page?

Updated October 25, 2017 11:09 AM

Magento2 - Cart Price Rule & Tier Price Issue

Updated August 06, 2018 15:09 PM

Magento 2: Hide tier price element

Updated December 09, 2017 20:09 PM

Grouped products pricing issue

Updated April 25, 2019 17:09 PM

Advanced pricing issue

Updated April 24, 2019 16:09 PM