I have a custom controller which can also update the cart:
namespace Vendor\Module\Controller\Index;
class Index extends \Magento\Checkout\Controller\Cart\UpdatePost
{
public function execute()
{
if (!$this->_formKeyValidator->validate($this->getRequest())) {
return $this->resultRedirectFactory->create()->setPath('checkout/cart/');
}
$this->_updateShoppingCart();
$quote = $this->_checkoutSession->getQuote();
$quote->collectTotals()->save();
$address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
$grandTotal = $address->getGrandTotal();
$subTotal = $address->getSubtotalInclTax();
$tax = $address->getTaxAmount();
$shipping = $address->getShippingInclTax();
// …
}
}
Judging by this code I thought it should be easy to get the correct amounts for the grand total (incl. tax), sub total (incl. tax), the total tax itself and the shipping (incl. tax).
However, with this code:
- the grand total is always without shipping
- the tax amount is always without the shipping tax
- the shipping amount is always zero
The cart of Magento itself shows the correct values for grand total, tax & shipping. But how can I get to these values myself in my custom controller?
$quote->getGrandTotal()
also excludes the shipping amount (shipping tax),$quote->getTaxAmount()
also excludes the shipping tax and$quote->getShippingInclTax()
returns0.00
. That's why I thought I would have to fetch the totals etc. from the billing or shipping address, in order to get the shipping amount included everywhere as well.