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

checkoutgrand-totalmagento2order-summary

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(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $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 = \Magento\Framework\App\ObjectManager::getInstance();
                $customerSession = $objectManager->create('Magento\Checkout\Model\Session');
                $cart = $objectManager->get('\Magento\Checkout\Model\Cart');  
                $billingAddress = $cart->getQuote()->getShippingAddress();
                $regon_id = $billingAddress->getData('region_id');
                $zipcode = $billingAddress->getData('postcode');
                $subtotal = $total->getTotalAmount('subtotal');

                $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/tax.log');
                $logger = new \Zend\Log\Logger();
                $logger->addWriter($writer);


                $deliveryId = $customerSession->getDeliveryid();

                $nyczipcode = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('deliveryoptions_customfee/deliveryoptions_customfee/deliveryoptions_zipcode');
                $nyczipcode = explode(',', $nyczipcode);

                $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
                $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;
    }

Best Answer

I did it before with this code.

if ($discountAmount >= 0) {
       $total = $quote->getBaseSubtotal();
       $quote->setSubtotal(0);
       $quote->setBaseSubtotal(0);

       $quote->setSubtotalWithDiscount(0);
       $quote->setBaseSubtotalWithDiscount(0);

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

       $canAddItems = $quote->isVirtual() ? ('billing') : ('shipping');
       foreach ($quote->getAllAddresses() as $address) {

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

       $address->setGrandTotal($priceTotal - $discountAmount + $taxTotal);
       $address->setBaseGrandTotal($priceTotal - $discountAmount + $taxTotal);

       $quote->setSubtotal($priceTotal);
       $quote->setBaseSubtotal($priceTotal);

       $quote->setSubtotalWithDiscount(
               ($priceTotal - $discountAmount)
       );
       $quote->setBaseSubtotalWithDiscount(
               ($priceTotal - $discountAmount)
       );

       $quote->setGrandTotal($priceTotal - $discountAmount + $taxTotal);
       $quote->setBaseGrandTotal($priceTotal - $discountAmount + $taxTotal);

       $quote->save();

       $quote->setGrandTotal($priceTotal - $discountAmount + $taxTotal)
                        ->setBaseGrandTotal($priceTotal - $discountAmount + $taxTotal)
                        ->setSubtotalWithDiscount($priceTotal - $discountAmount)
                        ->setBaseSubtotalWithDiscount($priceTotal - $discountAmount)
                        ->save();

       if ($address->getAddressType() == $canAddItems) {
               $address->setSubtotalWithDiscount($priceTotal - $discountAmount + $taxTotal);
               $address->setGrandTotal($priceTotal - $discountAmount + $taxTotal);
               $address->setBaseSubtotalWithDiscount($priceTotal - $discountAmount + $taxTotal);
               $address->setBaseGrandTotal($priceTotal - $discountAmount + $taxTotal);

               $address->setTaxAmount($taxTotal);
               $address->setBaseTaxAmount($taxTotal);

               if ($address->getDiscountDescription()) {
                     $address->setDiscountAmount(-($discountAmount));
                     // $address->setDiscountDescription($address->getDiscountDescription() . ', Gift Sets');
                     $address->setBaseDiscountAmount(-($discountAmount));
               } else {
                     $address->setDiscountAmount(-($discountAmount));
                     // $address->setDiscountDescription('');
                     $address->setBaseDiscountAmount(-($discountAmount));
               }
               $address->save();
            }
        }

     foreach ($quote->getAllItems() as $item) {
            $item->setDiscountAmount(0);
            $item->setBaseDiscountAmount(0)->save();
     }
}

Update : Yes, you will get wrong in the frontend side, cause you don't do it with event. Try to did it too with sales_quote_collect_totals_afterevent to change the grand total.

Related Topic