Skip to main content
added 198 characters in body
Source Link
dev_67Commerce
  • 1.2k
  • 10
  • 17

You can use like this in list.phtml:-

$cart = $objectManager->get('\Magento\Checkout\Model\Session')->getQuote();
$result = $cart->getAllVisibleItems();
$itemsIds = array();
foreach ($result as $cartItem) {
    array_push($itemsIds, $cartItem->getProduct()->getId());
}

print_r(in_array($productId, $itemsIds));

As an alternative you can also use Magento default feature:-

For individual settings, go to product details

Click Advanced Inventory

Maximum allowed inventory, and make it 1

You can use like this in list.phtml:-

$cart = $objectManager->get('\Magento\Checkout\Model\Session')->getQuote();
$result = $cart->getAllVisibleItems();
$itemsIds = array();
foreach ($result as $cartItem) {
    array_push($itemsIds, $cartItem->getProduct()->getId());
}

print_r(in_array($productId, $itemsIds));

You can use like this in list.phtml:-

$cart = $objectManager->get('\Magento\Checkout\Model\Session')->getQuote();
$result = $cart->getAllVisibleItems();
$itemsIds = array();
foreach ($result as $cartItem) {
    array_push($itemsIds, $cartItem->getProduct()->getId());
}

print_r(in_array($productId, $itemsIds));

As an alternative you can also use Magento default feature:-

For individual settings, go to product details

Click Advanced Inventory

Maximum allowed inventory, and make it 1

Source Link
dev_67Commerce
  • 1.2k
  • 10
  • 17

You can use like this in list.phtml:-

$cart = $objectManager->get('\Magento\Checkout\Model\Session')->getQuote();
$result = $cart->getAllVisibleItems();
$itemsIds = array();
foreach ($result as $cartItem) {
    array_push($itemsIds, $cartItem->getProduct()->getId());
}

print_r(in_array($productId, $itemsIds));