Excluding Shipping Costs from the Magento Cart Total
When it comes to displaying the total cost of items in a customer’s cart in Magento, you might be using the getGrandTotal() method like this:
echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal());
This works perfectly fine when the user is not logged in. However, if a user is logged in and there’s only one fixed shipping option available as you mentioned, Magento will automatically add this shipping cost to the total price.
To avoid including the shipping costs in your cart display for logged-in users with fixed shipping rates, follow these steps:
Understanding Magento’s Quote Totals
Magento calculates the grand_total by adding together several components, including tax and shipping. The specific calculation depends on the quote object’s state.
In your case, you’re dealing with a quote that has an associated customer id (since the user is logged in). When a customer is assigned to a quote, Magento automatically tries to apply any available fixed rate shipping methods based on the rate_model configured for each method.
Excluding Shipping from Magento’s Grand Total
To exclude the shipping cost from the displayed grand total, you need to calculate it manually without including the shipping charges. Here’s how:
First, get the quote object and initialize a new total instance:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$total = $quote->getTotals();
Next, extract the sub-total, tax, and discount amounts from the totals array:
$subTotal = $total['subtotal'];
$tax = $total['tax_amount'];
$discount = $total['discount_amount'];
Now, calculate the exclusive grand total by adding these values together. Note that you don’t include any shipping charges here:
$exclusiveGrandTotal = $subTotal + $tax + $discount;
Finally, format and display this calculated value:
echo $this->helper('checkout')->formatPrice($exclusiveGrandTotal);
This approach ensures that the displayed total cost of items in the cart excludes any fixed shipping charges, even for logged-in users.
Conclusion
In Magento 1.6.0.0, excluding fixed shipping costs from the cart grand total requires manual calculation when dealing with logged-in customers who have an assigned customer id to their quote objects.
By following this method and calculating the grand_total without including shipping charges, you can provide a more accurate representation of the item cost in the cart for your users. Remember that this solution assumes there is only one fixed rate shipping option available on your site.
For further guidance on Magento development best practices and tips, be sure to check out our blog at Laravel Company. We cover a wide range of topics to help you make the most of your e-commerce platform.