Magento – How to get cart item id using product id in magento2

cartmagento2mini-cart

I want to get cart item id using product id. For example, my product id is 600 and item id is 250.

using product id , want to get cart item id.

How to get it ?

please help me..

Best Answer

$productid = 600;
$quote = $this->checkoutHelper->getQuote();

foreach ($quote->getAllVisibleItems() as $_item) {
  $cartproductid = $_item->getProductId();
  if($productid==$cartproductid)
  {
    your_item_id = $_item->getId();
 }
}