简体   繁体   English

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

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

I am working with magento 2. I got php error when am using 我正在使用magento 2.我在使用时遇到了php错误

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

how to get cart item count in magento 2? 如何在洋红2中获得购物车项目?

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

Magento 2 provides 2 ways to show items count. Magento 2提供了两种显示项目计数的方法。 One shows the number of individual items in the cart, while the other one shows the total count of items in the cart. 一个显示购物车中单个商品的数量,而另一个显示购物车中商品的总数。

Let us say that cart helper is; 让我们说购物车助手是;

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

When you do: 当你这样做时:

echo $counter->getItemsCount(); echo $ counter-> getItemsCount();

it will show the number of individual items in the cart. 它将显示购物车中单个商品的数量。

If you want to show total items count, then use: 如果要显示总项数,请使用:

echo $counter->getSummaryCount(); echo $ counter-> getSummaryCount();

If you want to get total qty of the products in cart. 如果你想在购物车中获得产品的总数量。

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

Try this code 试试这个代码

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