0

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.

ratul keot
  • 45
  • 1
  • 10

3 Answers3

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

dev_67Commerce
  • 1,173
  • 9
  • 17
  • Can you please provide me the path to list.phtml?? – ratul keot Jan 18 '18 at 06: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

    – dev_67Commerce Jan 18 '18 at 06:42
  • I am using a theme. Will this effect because of that? – ratul keot Jan 18 '18 at 06: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. – dev_67Commerce Jan 18 '18 at 06: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 Jan 18 '18 at 06:55
  • I think you should follow this thread, answer by @Marius

    https://magento.stackexchange.com/questions/122287/how-to-create-simple-module-in-magento-2

    You do not need to worry about controller in your case, composer.json, registration.php and module.xml should be sufficient for you and then once these 3 files are created, just copy paste your list.phtml from vendor into the path shared above of your module

    – dev_67Commerce Jan 18 '18 at 07:05
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);
Suresh Chikani
  • 15,836
  • 11
  • 62
  • 99
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
    }
    

    }

Dinesh Yadav
  • 6,447
  • 2
  • 23
  • 50