Magento 1.9 – Price Doubled in Checkout When setGrandTotal is Applied

magento-1magento-1.9

I'm creating a module which applies discount based on Payment Method, programatically, everything works fine, until I call setGrandTotal and setBaseGrandTotal, for some reason, they double the price in the checkout page, but weirdly, when the order is finished, the price is correct.

enter image description here

My function:

foreach ($info->getQuote()->getAllAddresses() as $address) {
                $quote = $address->getQuote();
                $quoteData = $quote->getData(); // grand total
                if ($quoteData) {
                    $pixDiscount = Mage::getStoreConfig('payment/pix/extra_discount');
                $quoteData = $quote->getData();

                $totalWithDiscount = ((floatval($quoteData['grand_total'])) - ((floatval($pixDiscount) * floatval($quoteData['subtotal']) / 100)));

                $address->setSubtotal(0);
                $address->setBaseSubtotal(0);

                $address->setGrandTotal(0);
                $address->setBaseGrandTotal(0);

                $address->collectTotals();


                $quote->setGrandTotal($totalWithDiscount);
                $quote->setBaseGrandTotal($totalWithDiscount);

                $quote->save();
                $address->setQuote($quote);

                $address->setGrandTotal($quote->getGrandTotal());
                $address->setBaseGrandTotal($quote->getBaseGrandTotal());


                print_r($quote->getGrandTotal());
                //if (strlen($address->getDiscountDescription()) > 0) {
                //  $messages[] = $address->getDiscountDescription();
                //}
                //$messages[] = $discount['description'];


                $address->setDiscountDescription(
                    "Disconto teste"
                );
                $totalDiscount = 0;
                $totalDiscount = floatval($pixDiscount) * floatval($quoteData['subtotal']) / 100;
                $address->setDiscountAmount($totalDiscount);

                $address->save();
            }
        }

'''

I've tried dividing the results by two (but it affect the price after order is complete). Already debugged every var possible and all of them have the price correctly, I just don't have any more ideas of why this is happening.

Best Answer

Remove this line from the code and then clear the cache and then check.

$quote->save();

Related Topic