繁体   English   中英

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

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

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

之前有一个叫做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));
    ?>
<?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]

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

这就是你所需要的1.7如果你已经在运行法师:应用程序你没有真正做任何事情。

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

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

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

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

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

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

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

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

这对我有用...

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM