Skip to content

Magento 2 add product to existing order problem (tax summary)

i have a module that ads product to existing order but the problem is that when adding product to order tax summary is not updating and I see table sales_order_tax and sales_order_tax_item are not populated with new product info on tax.

Code that adds products to order:

    protected function _addProductToOrder($product, $order)
    {
        $orderItem = $this->orderFactory->create()->getItemsCollection()->getNewEmptyItem();
        $orderItem->setProduct($product);
        $orderItem->setQtyOrdered(1);
        
        // Set additional product information
        $orderItem->setName($product->getName());
        $orderItem->setSku($product->getSku());
        $storeId = $order->getStore()->getId();
        $orderItem->setStoreId($storeId);
        $orderItem->setProductId($product->getId());
        $orderItem->setWeight($product->getWeight());
        $orderItem->setIsVirtual(0);
    
        // Set custom price (promo or final)
        $promoPrice = $product->getPromoPrice();
        $priceInclTax = $promoPrice ? $promoPrice : $product->getFinalPrice();
    
        // Get the tax class ID and customer ID
        $taxClassId = $product->getTaxClassId();
        $customerId = $order->getCustomerId();
    
        // Calculate the tax rate
        $taxRate = $this->taxCalculation->getCalculatedRate($taxClassId, $customerId, $storeId);
    
        // Price excluding tax
        $priceExclTax = $priceInclTax / (1 + ($taxRate / 100));
        $taxAmount = $priceInclTax - $priceExclTax;
    
        // Set item prices and tax information
        //$orderItem->setBaseCost($priceExclTax); // Base cost without tax
        $orderItem->setPrice($priceExclTax);
        $orderItem->setPriceInclTax($priceInclTax);
        $orderItem->setBasePrice($priceExclTax);  // Base price excluding tax
        $orderItem->setBasePriceInclTax($priceInclTax); // Base price including tax
        $orderItem->setOriginalPrice($priceInclTax); // Original price excl tax
        $orderItem->setBaseOriginalPrice($priceExclTax); // Base original price excl tax

        // Set tax information
        $orderItem->setTaxAmount($taxAmount);
        $orderItem->setTaxPercent($taxRate);
        $orderItem->setBaseTaxAmount($taxAmount);

        // Set row totals
        $orderItem->setRowTotal($priceExclTax);
        $orderItem->setBaseRowTotal($priceExclTax);
        $orderItem->setRowTotalInclTax($priceInclTax);
        $orderItem->setBaseRowTotalInclTax($priceInclTax);
    
        // Add the item to the order
        $order->addItem($orderItem);
        $orderItem->setOrder($order);
        $orderItem->save();
        $order->save();
        
        return $orderItem;
    }

After that I am alos updating order totals like this:

        $order->setSubtotal($newSubtotal);
        $order->setBaseSubtotal($newSubtotal);
        $order->setSubtotalInclTax($newSubtotal + $newTaxAmount);
        $order->setBaseSubtotalInclTax($newSubtotal + $newTaxAmount);
        $order->setTaxAmount($newTaxAmount + $shippingTaxAmount);
        $order->setBaseTaxAmount($newTaxAmount + $baseShippingTaxAmount);
    
        // Calculate new grand total with the updated values
        $order->setGrandTotal($newSubtotal + $newTaxAmount + $shippingAmount + $shippingTaxAmount);
        $order->setBaseGrandTotal($newSubtotal + $newTaxAmount + $baseShippingAmount + $baseShippingTaxAmount);
        $order->setTotalItemCount($count);
        $order->setTotalQtyOrdered($count);
        // Save the updated order
        $order->save();

What is the reason? what am I missing?
Also if I want to edit order newly added products are not on the product list (ony items that were on when order was created).

Text