Opencart - Block adding Same product to the Cart

300 Views Asked by At

How to block a same Product being added to the cart twice?

Ex: Online Bookstore site has different books. I want the customer to be able to add a book (Ex; Book name : Cricket 911) only once to the cart and want to restrict adding the same book to the cart again.

Have anyone done this kind of a development before?

2

There are 2 best solutions below

0
On

When saving user cart information format it in this way:

[
    bookID: bookInfo
]

Then any same bookID will override previous selection and you will have unique keys

0
On

You need to edit the code in controller catalog/checkout/cart.php function add()

You need to get the products in cart, for that you can call the funtion of system/library/cart/cart.php getProducts() by

$products = $this->cart->getProducts();

You need to check if the product that is being added to the cart exists in the $products by comparing product_id

If the product exists, you can add error to $json['error']. All this processing should be done before "if (!$json) {" in the function add() so that the product isn't added to the cart.