0

How to create a module that will check if there is an item in the basket? Verification is needed on the product page.

1 Answers1

1

Try to use this below code :

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


public function getProductData()
{
    $productInfo = $this->_cart->getQuote()->getItemsCollection();
    foreach ($productInfo as $item){
       $item->getProductId();
       if($item->getProductId() == 'your_product_id')
       {
            echo "Item available in cart";
       }
       else
       {
            echo "Item not available in cart";
       }
    }
}
Rohan Hapani
  • 17,388
  • 9
  • 54
  • 96