1

I am creating certain group of products which has one quantity for each product. So, I want to disable the Add to Cart button if that same product is in cart and change the text of the button to Already in Cart. How can I achieve this?? I am using magento 2.2

Any help/suggestion is appreciated. Thank you.

1
  • have you find any solution for this? Commented Jun 17, 2021 at 6:52

3 Answers 3

3

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

7
  • Can you please provide me the path to list.phtml??
    – ratul keot
    Commented Jan 18, 2018 at 6:41
  • It is located here:- vendor\magento\module-catalog\view\frontend\templates\product\list.phtml But you will need to create a small module, and override it something like:- app/design/frontend/Companyname/Module/Magento_Catalog/templates/product/list.phtml Commented Jan 18, 2018 at 6:42
  • I am using a theme. Will this effect because of that?
    – ratul keot
    Commented Jan 18, 2018 at 6:46
  • Logically you should do it by creating a theme of your own and making the theme which you are using as a parent theme. And hence change in that child theme. Such that in future is your theme gets updated this change does not get impacted. Commented Jan 18, 2018 at 6:51
  • ok I am a Noob here. So i apologize for asking silly questions. But in which section do I put the above code in vendor\magento\module-catalog\view\frontend\templates\produc‌​t\list.phtml
    – ratul keot
    Commented Jan 18, 2018 at 6:55
1

You can check product exist or not in Quote by below code.

protected $checkoutSession;

public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    )
    {
        $this->checkoutSession = $checkoutSession;
     }

$yourProductId = ''; //Add your product Id here

$this->checkoutSession->getQuote()->hasProductId($yourProductId);
1
  • I have multiple products with quantity=1. Will this work for all? Thank you
    – ratul keot
    Commented Jan 18, 2018 at 6:41
1
  1. You can use the hasProductId function to check if a product is available in the cart or not.
  2. You should use the session factory to get the checkout session.
  3. Don't use the objectManager directly. Use dependency injection.

    private $checkoutSession;
    
    public function __construct(
        \Magento\Checkout\Model\SessionFactory $checkoutSession
    ) {
        $this->checkoutSession = $checkoutSession;
    }
    
    .....
    .....
    $productId = "1"; // Add your product id
    
    if ($this->checkoutSession->create()->getQuote()->hasProductId($productId)) {
        //product is available in the cart
    }
    

    }

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.