0

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.

  • you can get cart data from session – Nikhil Vaghela Mar 26 '18 at 8:44
  • Do you want to get total qty of all items? Or of the particular item? – Stepan Furman Mar 26 '18 at 8:44
  • qty of all items @stepan – sourabh yadav Mar 26 '18 at 9:23
  • Did you try what Nikhil advice? – Stepan Furman Mar 26 '18 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 – sourabh yadav Mar 26 '18 at 9:25
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\Customer\Model\Session');
  $cartQuote=$checkoutSession->getQuote();  
  $items=$cartQuote->getAllItems();
| improve this answer | |
  • is there any way to use above method using object manager – sourabh yadav Mar 26 '18 at 9:51
  • Yes you can get via objectManager (Please check update ans.) – Nikhil Vaghela Mar 26 '18 at 9:58
  • 1
    It should be '\Magento\Checkout\Model\Session' instead of '\Magento\Customer\Model\Session' – alexblum Jun 21 '19 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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