Limiting the quantity of selling products

I had this project where the client is selling software licence keys in his OpenCart store and the quantity of purchased products should be limited to only one product at a time. You can buy as many different products as you like, but the quantity per product is always 1. The store is running on OC version 2.0.3.1 and here is how limiting the quantity of selling products works.

Follow these steps if you find yourself in a similar situation and need limiting the quantity of selling products:

1. Locate and open the following file: system/library/cart.php

2. Find the following line of code:

$this->session->data['cart'][$key] += (int)$qty;

3. Replace it with:

$this->session->data['cart'][$key] = (int)$qty;

Very simple, yes. But this is only to limit the product that customers add to cart to the value of 1. If you go to cart page you could still modify the quantity of the added product there by inserting new value and pressing the refresh button. To fix that:

4. Open the file located in: /catalog/controller/checkout/cart.php

5. Find the following lines of code:


if (!empty($this->request->post['quantity'])) {
            foreach ($this->request->post['quantity'] as $key => $value) {
                $this->cart->update($key, $value);
            }

6. Update the existing forloop with the new line of code so it looks like this:


if (!empty($this->request->post['quantity'])) {
            foreach ($this->request->post['quantity'] as $key => $value) {
                $value=1;
                $this->cart->update($key, $value);
            }

This will reset the quantity to 1 even if the customer try to update the quantity on the cart page.

By janoshke

Web developer and IT consultant. Freelancer with full respect for OpenCart and WordPress. Gamer, (ex)drummer and parent.

2 comments

Leave a Reply