I know grand total is calculated on quote, I would like to find out is there a way to create an order object, add items to it and then get it's grand total without just manually adding item prices to get sum. Also this needs to be done without saving the order.
Magento – Calculate grand total on order
grand-totalorders
Related Solutions
To sum it up, order totals aren't stored on the order object the same way as on the quote object. The reason is that the result of the dynamic quote totals are stored in the order table. You're right in it being a bad thing to get the quote totals from the order, as things like credit memos affect order totals.
The quote totals also has some extra attributes available for tax calculation that doesn't exist in the order totals procedure and that might depend on session variables.
So, bottom line: The order totals is the different values you can get*() from the order object.
The invoice and credit memo can collectTotals() because they subsequently modify the order totals (amount_paid, amount_invoiced, ..). Totals which are in fact values from sales_order_flat
.
The block adminhtml/sales_order_totals
used in the order view loops over getTotals('')
which can cause some terminology confusion. These totals are in fact of type Varien_Object and holds static values from the database. Adding a row in there is possible but not at all as straight-forward as with the quote totals.
I was able to get this working using only javascript. A similar solution is below:
define([
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/resource-url-manager',
'Magento_Checkout/js/model/payment-service',
'mage/storage',
'Magento_Checkout/js/model/payment/method-converter',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/model/error-processor'
], function(
quote,
resourceUrlManager,
paymentService,
storage,
methodConverter,
fullScreenLoader,
errorProcessor
) {
'use strict';
return function(taxExemptValue) {
var payload = {
addressInformation: {
shipping_address: quote.shippingAddress(),
billing_address: quote.billingAddress(),
shipping_method_code: quote.shippingMethod().method_code,
shipping_carrier_code: quote.shippingMethod().carrier_code
}
};
fullscreenLoader.startLoader();
storage.post(
resourceUrlManager.getUrlForSetShippingInformation(quote),
JSON.stringify(payload)
).done(
function (response) {
if (taxExemptValue == 1) {
var currentTax = response.totals.tax_amount;
response.totals.shipping_tax_amount = 0;
response.totals.tax_amount = 0;
response.totals.base_tax_amount = 0;
response.totals.base_grand_total -= currentTax;
response.totals.grand_total -= currentTax;
}
quote.setTotals(response.totals);
paymentService.setPaymentMethods(methodConverter(response.payment_methods));
fullscreenLoader.stopLoader();
}
).fail(
function (response) {
errorProcessor.process(response);
fullscreenLoader.stopLoader();
}
);
};
This would be called when the tax exempt field is changed, passing in it's value.
Best Answer
To calculate and process the grand total you need to follow these
This is working on checkout and cart page to update the grandtotal. Hope this will help you and you find a correct solution.