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

时间:2016-05-10 09:41:09

标签: php magento2

我正在使用magento 2.我在使用

时遇到了php错误
echo Mage::helper(‘checkout/cart’)->getCart()->getItemsCount();

如何在magento 2中获得购物车项目数?

5 个答案:

答案 0 :(得分:3)

<Configuration status="WARN">
  <Appenders>
    <!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
    <RandomAccessFile name="RandomAccessFile" fileName="async.log" immediateFlush="false" append="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
      </PatternLayout>
    </RandomAccessFile>
  </Appenders>
  <Loggers>
    <Root level="info" includeLocation="false">
      <AppenderRef ref="RandomAccessFile"/>
    </Root>
  </Loggers>
</Configuration>

答案 1 :(得分:2)

Magento 2提供了两种显示项目计数的方法。一个显示购物车中单个商品的数量,而另一个显示购物车中商品的总数。

让我们说购物车助手是;

  

$ helper = $ this-&gt; helper(&#39; \ Magento \ Checkout \ Helper \ Cart&#39;);

当你这样做时:

  

echo $ counter-&gt; getItemsCount();

它将显示购物车中单个商品的数量。

如果要显示总项数,请使用:

  

echo $ counter-&gt; getSummaryCount();

答案 2 :(得分:0)

如果要在购物车中获取全部产品的数量。

$helper = $this->helper('\Magento\Checkout\Helper\Cart');
$helper->getItemsQty(); //get total qty of the cart

答案 3 :(得分:0)

$objmanager = \Magento\Framework\App\ObjectManager::getInstance();
$session =  $objmanager->get("Magento\Checkout\Model\Session");
$quote =$session->getQuote();
$qty =  $quote->getItemsSummaryQty();

答案 4 :(得分:-4)

试试此代码

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