1

I am currently using this code and it is working fine,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$totalQuantity = $cart->getQuote()->getItemsQty();    

but it is not working on few category pages as well as Home page in my theme. Please help.

5
  • you can get cart data from session Commented Mar 26, 2018 at 8:44
  • Do you want to get total qty of all items? Or of the particular item? Commented Mar 26, 2018 at 8:44
  • qty of all items @stepan Commented Mar 26, 2018 at 9:23
  • Did you try what Nikhil advice? Commented Mar 26, 2018 at 9:24
  • @Nikhil actually the code i am using is present in design section of theme so i don't know how to use session there Commented Mar 26, 2018 at 9:25

2 Answers 2

3

use below code

public function __construct(
    \Magento\Customer\Model\Session $currentCustomer
) { 
    $this->checkoutSession = $checkoutSession;   
 }
 
 
 $cartQuote=$this->checkoutSession->getQuote();  
 $items=$cartQuote->getAllItems();

Or

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
  $cartQuote=$checkoutSession->getQuote();  
  $items=$cartQuote->getAllItems();
3
  • is there any way to use above method using object manager Commented Mar 26, 2018 at 9:51
  • Yes you can get via objectManager (Please check update ans.) Commented Mar 26, 2018 at 9:58
  • 1
    It should be '\Magento\Checkout\Model\Session' instead of '\Magento\Customer\Model\Session'
    – alexblum
    Commented Jun 21, 2019 at 6:56
1

I get what i need using these code but thanku all

<block class="<Vendor>\Cart\Block\Cart" before="-" template="<Vendor>_Cart::cart-panel.phtml" name="cart-panel" cacheable="false" />

in my theme default.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.