In Minicart, we have products

<input id="cart-602-qty" maxlength="12" name="cart[602][qty]" size="4" type="number">

This is not Product Id. So how to get actual product id from cart item id?

Lastly, can override template & put product id in hidden.

4 Answers 4


Try this

public function __construct(
    \Magento\Checkout\Model\Cart $cart 
) {
    $this->_cart = $cart;  

public function getProductData()
    $productInfo = $this->_cart->getQuote()->getItemsCollection();
    //$productInfo = $this->_cart->getQuote()->getAllItems(); /*****For All items *****/
    foreach ($productInfo as $item){
       echo $item->getProductId();


Below is answer

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productData = $_objectManager->get('Magento\Quote\Model\Quote\Item')->load(<item id>);
echo $productData->getProductId();
  • 5
    Direct use of an Object manager is not an appropriate answer. Commented Mar 16, 2017 at 4:59
  • 1
    I know. This is in general.
    – Jackson
    Commented Mar 16, 2017 at 5:41
  • 7
    Then don't paste code like this or you should mention this in note. because new learner will post this code directly. Commented Mar 16, 2017 at 5:52

If you have an cart item of the type \Magento\Quote\Api\Data\CartItemInterface and you want to get the product ID you might find this useful:

public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
) {
    $this->productRepositoryInterface = $productRepositoryInterface;

public function someFunction(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    $product = $this->productRepositoryInterface->get($cartItem->getSku());
    $productId = $product->getId();
    // ...

This function loads a product based on the SKU we can retrieve from the cart item.


Code for get added product in cart by the user in login or without login

Note- If login then give current product quantity otherwise give overall quantity

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$context = $objectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
$_product = $objectManager->get('\Magento\Checkout\Model\Cart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
foreach($itemsCollection->getData() as $item) 
                  echo (int)$item['qty'];
  • How can I get $_product value? Commented Aug 24, 2022 at 13:54
  • @MuhammadMuizAther we have updated the code please check it again. Hope it is working fine for you. If fine the please vote me Commented Apr 26, 2023 at 10:21

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.