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 Answer 1

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";
       }
    }
}
8
  • Where should I put this code? Commented Apr 8, 2019 at 10:14
  • you can add in your block or controller. Commented Apr 8, 2019 at 10:15
  • What structure should be? I just do not understand what should be in the model and what should be in the block, etc. Commented Apr 8, 2019 at 10:23
  • Wherer you want to access this value? Commented Apr 8, 2019 at 10:30
  • On the product page Commented Apr 8, 2019 at 10:31

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.