Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?>
like image 470
TheBlackBenzKid Avatar asked Jan 19 '12 11:01

TheBlackBenzKid


People also ask

How do I get the total cart in Magento 2?

Get the number of items in cart and total quantity in cart. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $totalItems = $cart->getQuote()->getItemsCount(); $totalQuantity = $cart->getQuote()->getItemsQty();

Where is Phtml in Magento?

Root template <Magento_Theme_module_dir>/view/base/templates/root. phtml is the root template for all storefront pages in the Magento application. This file can be overridden in a theme just like any other template file. Unlike other templates, root.


2 Answers

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));
    ?>
like image 114
TheBlackBenzKid Avatar answered Sep 28 '22 20:09

TheBlackBenzKid


<?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]

like image 21
Andres Separ Avatar answered Sep 28 '22 18:09

Andres Separ