Magento – Magento 2: How to get Product Id from Cart Item Id

cartmagento2magento2.1.5

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.

Best Answer

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();

    }
}