繁体   English   中英

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

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

我正在使用magento 2.我在使用时遇到了php错误

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

如何在洋红2中获得购物车项目?

    $counter = $this->helper('\Magento\Checkout\Helper\Cart'); 
    echo $counter->getItemsCount();

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

让我们说购物车助手是;

$ helper = $ this-> helper('\\ Magento \\ Checkout \\ Helper \\ Cart');

当你这样做时:

echo $ counter-> getItemsCount();

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

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

echo $ counter-> getSummaryCount();

如果你想在购物车中获得产品的总数量。

$helper = $this->helper('\Magento\Checkout\Helper\Cart');
$helper->getItemsQty(); //get total qty of the cart
$objmanager = \Magento\Framework\App\ObjectManager::getInstance();
$session =  $objmanager->get("Magento\Checkout\Model\Session");
$quote =$session->getQuote();
$qty =  $quote->getItemsSummaryQty();

试试这个代码

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

暂无
暂无

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

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