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

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";
       }
    }
}
  • Where should I put this code? – Рома Лытарь Apr 8 '19 at 10:14
  • you can add in your block or controller. – Rohan Hapani Apr 8 '19 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. – Рома Лытарь Apr 8 '19 at 10:23
  • Wherer you want to access this value? – Rohan Hapani Apr 8 '19 at 10:30
  • On the product page – Рома Лытарь Apr 8 '19 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.