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

发布于 2024-12-27 17:49:03 字数 612 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

与酒说心事 2025-01-03 17:49:03

我想,之前有一个叫 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));
    ?>

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));
    ?>
以往的大感动 2025-01-03 17:49:03
<?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>[

输出:

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

.$this->helper('core')->formatPrice($cartTotal, false).']</span> </a>'; ?>

输出:

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

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

Output:

Your basket
3 Items [$32.5]

.$this->helper('core')->formatPrice($cartTotal, false).']</span> </a>'; ?>

Output:

Your basket
3 Items [$32.5]

故事还在继续 2025-01-03 17:49:03

<代码>getItemsCount();
回显$_cartQty;
?>

如果你已经运行了 mage:app,那么这就是 1.7 所需要的,没有它你什么也做不了。

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

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

用心笑 2025-01-03 17:49:03

使用辅助对象获取当前购物车对象,然后计算购物车对象中的商品数量。

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

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

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/

风月客 2025-01-03 17:49:03

您可以在此处找到您的购物车模板:

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

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()); ?>
左耳近心 2025-01-03 17:49:03

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

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.

幸福还没到 2025-01-03 17:49:03
<?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));
    ?>

这对我有用,谢谢...

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

醉生梦死 2025-01-03 17:49:03

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

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

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;?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文