Magento - 如何在header.phtml中获取购物车商品总数

15
我正在使用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 ?>

如果你var_dump $cartQty,你会得到什么? - Kristian Hildebrandt
我怎么获取数量?可以帮忙吗? - Jenith Samuel
8个回答

39

之前有个名叫SUHUR的人回答了一个链接,我想用这个答案来奖励他,但是他好像把自己的帖子删掉了?

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

我修改了我的代码,现在在.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));
    ?>

以上方法对我无效,但是这个可以:http://www.richardcastera.com/blog/magento-get-the-total-price-of-items-currently-in-the-cart - logic-unit
嗨,感谢您的帖子和链接,我希望其他人也会觉得有用。您能告诉我是哪个版本吗? - TheBlackBenzKid

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

输出:

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


1
添加一些文本来演示代码,或者可能是输出样例。 - JoshDM
我怎么获取数量?可以帮忙吗? - Jenith Samuel
1
嗨@JenithSamuel,物品数量存储在$cartItemsCount中。 - Andres Separ

5

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

如果你已经在运行mage:app,那么这就是1.7所需要的全部内容。此外,此代码仅显示“项”计数,而不是数量。


3
你可以在这里找到你的购物车模板:
YOURSITE/app/design/frontend/YOURTHEME/default/template/checkout/cart/minicart.phtml

在一个类名为.count的区块中,你会看到以下代码片段:
<span class="count"><?php echo $_cartQty; ?></span>

用以下这段代码代替它,你将会看到总计金额的显示结果:
<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>

如何显示购物车的总重量? - Gem
@Rathinam 抱歉,我不知道,我已经好几年没用Magento了,谢天谢地! :) - Pixelomo

3

我怎么获取数量?可以帮忙吗? - Jenith Samuel

1

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


1
谁在乎呢?Magento是我迄今为止遇到的最烂的电子商务系统。我们放弃了使用它的计划。 - TheBlackBenzKid

0

在phtml文件中使用以下代码获取购物车中的商品数量。

<?php $helper = $this->helper('\Magento\Checkout\Helper\Cart');
      $noCartItems= $helper->getSummaryCount();
      echo $noCartItems;?>

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

这对我有用,谢谢...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接