[英]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.