How to create a module that will check if there is an item in the basket? Verification is needed on the product page.
Asked
Active
Viewed 969 times
1 Answers
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
-
Where should I put this code? – Рома Лытарь Apr 08 '19 at 10:14
-
you can add in your block or controller. – Rohan Hapani Apr 08 '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 08 '19 at 10:23
-
Wherer you want to access this value? – Rohan Hapani Apr 08 '19 at 10:30
-
On the product page – Рома Лытарь Apr 08 '19 at 10:31
-
No. I mean in phtml file or contorller etc.? Do you have custom block which is used in product page. – Rohan Hapani Apr 08 '19 at 10:32
-
Let us continue this discussion in chat. – Рома Лытарь Apr 08 '19 at 10:33
-
@РомаЛытарьYou can accept and upvote answer. If it's helpful for you. – Rohan Hapani Apr 08 '19 at 13:06