Skip to content

Magento 2 How to grandtotal in order summary set via model?

I am using Magento2 and in order summary, some customize in tax after grandtotal display old not new so anyone idea to setGrandTotal via a model.

If anyone idea reply to me

Below my code added in this file /vendor/magento/module-tax/Model/Sales/Total/Quote/Tax.php

this is my code added here but in log i get grandtotal proper but in froented side old total display.

public function collect(
        MagentoQuoteModelQuote $quote,
        MagentoQuoteApiDataShippingAssignmentInterface $shippingAssignment,
        MagentoQuoteModelQuoteAddressTotal $total
    ) {
        $this->clearValues($total);
        if (!$shippingAssignment->getItems()) {
            return $this;
        }

        $baseTaxDetails = $this->getQuoteTaxDetails($shippingAssignment, $total, true);
        $taxDetails = $this->getQuoteTaxDetails($shippingAssignment, $total, false);

        //Populate address and items with tax calculation results
        $itemsByType = $this->organizeItemTaxDetailsByType($taxDetails, $baseTaxDetails);
        if (isset($itemsByType[self::ITEM_TYPE_PRODUCT])) {
            $this->processProductItems($shippingAssignment, $itemsByType[self::ITEM_TYPE_PRODUCT], $total);
        }

        if (isset($itemsByType[self::ITEM_TYPE_SHIPPING])) {
            $shippingTaxDetails = $itemsByType[self::ITEM_TYPE_SHIPPING][self::ITEM_CODE_SHIPPING][self::KEY_ITEM];
            $baseShippingTaxDetails =
                $itemsByType[self::ITEM_TYPE_SHIPPING][self::ITEM_CODE_SHIPPING][self::KEY_BASE_ITEM];
            $this->processShippingTaxInfo($shippingAssignment, $total, $shippingTaxDetails, $baseShippingTaxDetails);
        }

        //Process taxable items that are not product or shipping
        $this->processExtraTaxables($total, $itemsByType);

        //Save applied taxes for each item and the quote in aggregation
        $this->processAppliedTaxes($total, $shippingAssignment, $itemsByType);

        if ($this->includeExtraTax()) {
            $total->addTotalAmount('extra_tax', $total->getExtraTaxAmount());
            $total->addBaseTotalAmount('extra_tax', $total->getBaseExtraTaxAmount());
        }


        //      custom code  for        

            if($regon_id==43){

                $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                $customerSession = $objectManager->create('MagentoCheckoutModelSession');
                $cart = $objectManager->get('MagentoCheckoutModelCart');  
                $billingAddress = $cart->getQuote()->getShippingAddress();
                $regon_id = $billingAddress->getData('region_id');
                $zipcode = $billingAddress->getData('postcode');
                $subtotal = $total->getTotalAmount('subtotal');

                $writer = new ZendLogWriterStream(BP . '/var/log/tax.log');
                $logger = new ZendLogLogger();
                $logger->addWriter($writer);


                $deliveryId = $customerSession->getDeliveryid();

                $nyczipcode = $objectManager->get('MagentoFrameworkAppConfigScopeConfigInterface')->getValue('deliveryoptions_customfee/deliveryoptions_customfee/deliveryoptions_zipcode');
                $nyczipcode = explode(',', $nyczipcode);

                $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
                $connection = $resource->getConnection();
                $tcr = $resource->getTableName('tax_calculation_rate');
                $taxrate = "select rate from " . $tcr . " WHERE tax_postcode = $zipcode";
                $result = $connection->fetchAll($taxrate);


                if (!in_array($zipcode, $nyczipcode)) {
                    if($deliveryId==11){
                    $newsubvalue = $subtotal + 200.00;
                    $taxamount = $newsubvalue / 100 * $result[0]["rate"];


                $total->setTaxAmount($taxamount);
                $total->setBaseTaxAmount($taxamount);

                $setGrandTotal = $newsubvalue + $taxamount;


                $total->setGrandTotal($setGrandTotal);
                $total->setBaseGrandTotal($setGrandTotal);

                    $logger->info("tax if here ".$subtotal);
                    $logger->info("tax if here ".$taxamount);
                    $logger->info("tax if here ".$setGrandTotal);
                    }
                    else {
                        $taxamount = $subtotal / 100 * $result[0]["rate"];
                        $setGrandTotal = $subtotal + $taxamount;

                $total->setTaxAmount($taxamount);
                $total->setBaseTaxAmount($taxamount);            
                $total->setGrandTotal($setGrandTotal);
                $total->setBaseGrandTotal($setGrandTotal);

                $logger->info("tax else dddddhere ".$subtotal);
                $logger->info("tax else dddddhere ".$taxamount);
                $logger->info("tax else dddddhere ".$setGrandTotal);
                   }

                } else {
                    $setGrandTotal = $subtotal + $taxamount;
                    $taxamount = $subtotal / 100 * $result[0]["rate"];
                $total->setTaxAmount($taxamount);
                $total->setBaseTaxAmount($taxamount);                
                $total->setGrandTotal($setGrandTotal);
                $total->setBaseGrandTotal($setGrandTotal);

                    $logger->info("tax else here ".$subtotal);
                    $logger->info("tax else here ".$taxamount);                    
                    $logger->info("tax else here ".$setGrandTotal);
                }


            }


        return $this;
    }