Magento - How to get cart items total in header.phtml
Asked Answered
L

8

15

I am using Magento eCommerce and I have modified my header.phtml via the Blank template. Code, this is my code but it shows blank.

 <?php $cartQty = $this->getSummaryCount() ?>
    <?php if ($cartQty>0): ?>

            <?php if ($cartQty==1): ?>
                <?php echo $this->__('<a class="cartgo" href="%s">(1 ITEM)</a>', $this->getUrl('checkout/cart')) ?>
            <?php else: ?>
                <?php echo $this->__('<a class="cartgo" href="%s">(%s ITEMS)</a>', $this->getUrl('checkout/cart')) ?>
            <?php endif ?>


    <?php endif ?>
Limoges answered 19/1, 2012 at 11:29 Comment(2)
what do you get if you var_dump $cartQty?Underrate
How i get the Quantity??can please helpCause
L
39

There was an answer to a link before by someone called SUHUR I think, I was going to reward him with the answer but it seems he deleted his own post?

He linked to this: http://nothingtopost.wordpress.com/tag/how-to-get-total-cart-item-in-magento/

I modified my code and this works now on .phtml files.

<?php
      $count = $this->helper('checkout/cart')->getSummaryCount();  //get total items in cart
      $total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
      if($count==0)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(0 ITEMS)</a>',$count);
      }
      if($count==1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(1 ITEM)</a>',$count);
      }
      if($count>1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(%s ITMES)</a>',$count);
      }
      echo $this->__('', $this->helper('core')->formatPrice($total, false));
    ?>
Limoges answered 19/1, 2012 at 11:48 Comment(2)
The above didn't work for me, but this did: richardcastera.com/blog/…Gyrocompass
Hi thanks for the post and link, I hope others find it useful. Can you tell me what version?Limoges
L
9
<?php
    $cartTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
    $cartItemsCount = Mage::helper('checkout/cart')->getCart()->getItemsCount();
    $cartSuffix = ($cartItemsCount != 1) ? 's' : '';

    echo '<a class="cartgo" href="'.$this->getUrl('checkout/cart').'">
              <strong>'.$this->__('Your basket').'</strong><br />'.
              $this->__('(%s) Item%s', $cartItemsCount, $cartSuffix).
              '<span>[$'.$this->helper('core')->formatPrice($cartTotal, false).']</span>
          </a>';
?>

Output:

Your basket
3 Items [$32.5]

Legofmutton answered 12/8, 2013 at 14:8 Comment(3)
Add some text to walk through the code, or maybe a sample of the output.Hunch
How i get the Quantity??can please helpCause
Hi @JenithSamuel the number of items are stored in $cartItemsCountLegofmutton
C
5

<?php $_cartQty = Mage::getSingleton('checkout/cart')->getItemsCount(); echo $_cartQty; ?>

thats all you need for 1.7 if your already running the mage:app which you can't do anything without really.

furthermore, this only shows "item" count, not quantity.

Carincarina answered 14/5, 2013 at 18:9 Comment(0)
L
3

Use the helper object to get the current cart object, and then count the number of items in the cart object.

echo Mage::helper('checkout/cart')->getCart()->getItemsCount();

More from http://www.douglasradburn.co.uk/how-to-get-number-of-cart-items-in-magento/

Littlejohn answered 3/2, 2015 at 4:19 Comment(1)
How i get the Quantity??can please helpCause
P
3

You can find your cart template here:

YOURSITE/app/design/frontend/YOURTHEME/default/template/checkout/cart/minicart.phtml

Within a span with the class of .count you'll see this snippet:

<span class="count"><?php echo $_cartQty; ?></span>

Replace it with this snippet and you'll get the grand total displayed instead:

<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>
Peale answered 5/1, 2016 at 11:6 Comment(2)
How can i display cart total weight?Analcite
@Rathinam sorry no idea, I haven't used Magento in years, thankfully! :)Peale
S
1

When linking to a cart, you should really use Mage::helper('checkout/cart')->getCartUrl(). The example given would not work if your site is hosted in a sub-domain.

Speiss answered 2/11, 2012 at 15:48 Comment(1)
Who cares? Magento is the biggest pile of crap eCommerce system I have come across to date. We scrapped our plans for it.Limoges
E
0
<?php
      $count = $this->helper('checkout/cart')->getSummaryCount();  //get total items in cart
      $total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
      if($count==0)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(0 ITEMS)</a>',$count);
      }
      if($count==1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(1 ITEM)</a>',$count);
      }
      if($count>1)
      {
        echo $this->__('<a href="/checkout/cart" class="cartgo">(%s ITMES)</a>',$count);
      }
      echo $this->__('', $this->helper('core')->formatPrice($total, false));
    ?>

this works for me thanx...

Euplastic answered 28/3, 2013 at 10:6 Comment(0)
A
0

Use the below code in phtml file to get the no of items in cart.

<?php $helper = $this->helper('\Magento\Checkout\Helper\Cart');
      $noCartItems= $helper->getSummaryCount();
      echo $noCartItems;?>
Anaemia answered 16/9, 2022 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.