I am working with magento 2. I got php error when am using
echo Mage::helper(‘checkout/cart’)->getCart()->getItemsCount();
how to get cart item count in magento 2?
$counter = $this->helper('\Magento\Checkout\Helper\Cart');
echo $counter->getItemsCount();
Magento 2 provides 2 ways to show items count. 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');
When you do:
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();
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));
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.