Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

How to set access to add/purchase only one item to the cart in Opencart?

I want to allow customers to buy only one product from my store. Customer can add add only one product to the cart and the quantity also should be one only but current opencart system is adding multiple products to the cart. How can I create a system like this in Opencart 1.5.5 ?

Upvotes: 1

Views: 1822

Answers (1)

Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

I have created the system using this forum post: http://forum.opencart.com/viewtopic.php?t=28181

  1. EDIT: system/library/cart.php

  2. FIND:

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

  3. REPLACE WITH:

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

Then

  1. EDIT: system/library/cart.php

2a. FIND (1.4.x):

if (!$options) {

2b. FIND (1.5.x):

if (!$option) {

  1. BEFORE, ADD:

$this->clear();

This will set customers to add only one product to the cart. But when you update the quantity from the cart page, it will be updated to given quantity. To fix that, we should change code in /catalog/controller/checkout/cart.php

Find if (!empty($this->request->post['quantity'])) at line 13 in /catalog/controller/checkout/cart.php

Replace the existing forloop like below. I means set value 1 to $value inside the loop. It will reset the quantity to 1 even when the customer try to update the quantity in cart page.

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

Upvotes: 1

Related Questions