php Magento - 如何在 header.phtml 中获取购物车项目总数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8925271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:47:43  来源:igfitidea点击:

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

phpmagentoxhtmlcart

提问by TheBlackBenzKid

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.

我正在使用 Magento 电子商务,并且通过空白模板修改了我的 header.phtml。代码,这是我的代码,但显示为空白。

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

回答by TheBlackBenzKid

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?

之前有个叫 SUHUR 的人回答了一个链接,我想,我打算用答案奖励他,但他似乎删除了自己的帖子?

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

他链接到这个:http: //nothingtopost.wordpress.com/tag/how-to-get-total-cart-item-in-magento/

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

我修改了我的代码,现在可以在 .phtml 文件上使用。

<?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));
    ?>

回答by Andres Separ

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

您的购物篮
3 件商品 [$32.5]

回答by dubrod

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

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

如果您已经在运行 mage:app,那么这就是 1.7 所需要的全部内容,如果没有,您将无法执行任何操作。

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

此外,这仅显示“项目”计数,而不是数量。

回答by Pixelomo

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 .countyou'll see this snippet:

在与.count您的班级的跨度内,您将看到以下代码段:

<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()); ?>

回答by d4nyll

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

使用helper 对象获取当前cart 对象,然后统计cart 对象中的商品数量。

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

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

更多来自http://www.douglasradburn.co.uk/how-to-get-number-of-cart-items-in-magento/

回答by Richard

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.

链接到购物车时,您应该真正使用Mage::helper('checkout/cart')->getCartUrl(). 如果您的站点托管在子域中,则给出的示例将不起作用。

回答by user2219206

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

这对我有用thanx ...