Magento get cart single item price incl. tax

Vlad Preda picture Vlad Preda · Feb 28, 2013 · Viewed 32.6k times · Source

I have a pretty weird issue, I hope someone can help me with this.

Here are the major config settings that influence my problem:

  • Catalog prices in admin panel are shown including tax
  • Catalog prices in frontend are shown including tax
  • Items in shopping cart are shown excluding tax (so it's displayed separately near the subtotal).

Everything is working fine so far. The problem comes in a custom ajax mini cart module. I grab the collection of items from the cart, but, since I'm getting the price from the shopping cart item, I get it without tax.

Here is some code to exemplify what I mean. I will assume a 20% tax and a product that has the admin price (including tax) set to 120$, an option that costs 60$ (also including tax). Excluding tax these would be 100$ and 50$. I want to get the price + option + tax => 180$

$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
    echo $item->getPrice(); // 150$ - price excluding tax
    echo $item->getPriceInclTax(); // 150$ - price excluding tax
    echo $item->getProduct()->getPrice(); // 120$ price including tax, BUT without the customer selected options.
}

PS: The custom option I am talking about is user selected, for example an install checkbox that adds +50$ to the price of the product.

Answer

Gaurang picture Gaurang · Mar 12, 2013
- Get products id, name, price, quantity, etc. present in your cart.
- Get number of items in cart and total quantity in cart.
- Get base total price and grand total price of items in cart.

Get all items information in cart
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";           
}

Get total items and total quantity in cart
$totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
$totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

Get subtotal and grand total price of cart
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();