Post by TC2021 » Tue Nov 30, 2021 11:30 pm

Hello folks:

I'm really hoping someone can help me out here.. I'm using opencart 3.0.3.8 to sell downloadable products.. So I don't really have a need for quantities of greater than 1.. in the product pages, when the user clicks add to cart, I'd like the function to be, on the first click, the product gets added to the cart with a quantity of 1.. thereafter, any subsequent clicks on the "add to cart" button for that product can add to cart, but the tally remains at one for that item.. I've checked the system/library/cart/cart.php to comment out a section which seems to indicate to add 1 unit to the product_id but it doesn't appear the change has any bearing..

I know there are quantity limit scripts out there that folks are selling but they are all done in the checkout cart phase.. I don't want that.. I want the the limitation to be made at the category page when the user clicks on the cart button.. keep in mind the client is purchasing downloadables and they never need more than 1 unit..

If I can get the above solved, that would be great.. a bonus would be that, if the user is a guest, but has an account and logs in with previous items in the cart from a past visit being the same as the ones they added as a guest, these items do not add together but merely remain at quantity 1..

I'm hoping someone can point me in the right direction to look..

I don't code for a living, but I can make changes and test results to get by.

I would appreciate any help.
Tim.

Newbie

Posts

Joined
Thu Nov 18, 2021 3:10 am

Post by moshair » Wed Dec 01, 2021 7:14 am

Hello,

The easy way without an extension is to disable add to cart button after clicking on it, but the user can add the same item again by refreshing the page ;(

edit
catalog/view/theme/default/template/product/category.twig

change

Code: Select all

<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}')"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
to

Code: Select all

 <button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');this.disabled=1"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
Do the same thing on the other pages that show add to cart button like search.twig, compare.twig, special.twig ... etc

On the product page you can hide Qty input by editing catalog/view/theme/default/template/product/product.twig and change

Code: Select all

<label class="control-label" for="input-quantity">{{ entry_qty }}</label>
 <input type="text" name="quantity" value="{{ minimum }}" size="2" id="input-quantity" class="form-control" />
to

Code: Select all

 <input type="hidden" name="quantity" value="{{ minimum }}" size="2" id="input-quantity" class="form-control" />
And to disable changing the quantity on the cart page:
edit
catalog/view/theme/default/template/checkout/cart.twig

remove

Code: Select all

<input type="text" name="quantity[{{ product.cart_id }}]" value="{{ product.quantity }}" size="1" class="form-control" />
and

Code: Select all

<button type="submit" data-toggle="tooltip" title="{{ button_update }}" class="btn btn-primary"><i class="fa fa-refresh"></i></button>
Hope that was correct and helped you.

New member

Posts

Joined
Sun Jul 21, 2019 3:27 pm

Post by TC2021 » Wed Dec 01, 2021 11:31 pm

Thanks for the reply Moshair.

Is there not a way to limit the quantity at the cart database code?

For instance, if let's say a guest session clicks add to cart, the first click goes and adds the product to the cart.. if the guest clicks add to cart again, can you not insert a condition in somewhere to say "that part is already in the cart, disregard adding to the quantity?

It just seems like this would be doable but I'm not sure where to look to insert this code..

Thanks again for your time.
Tim

Newbie

Posts

Joined
Thu Nov 18, 2021 3:10 am

Post by TC2021 » Wed Dec 01, 2021 11:36 pm

Forgot to add.. I've modified the code in that the quantity field are hidden in the checkout section.. as the product is either in the cart at quantity 1 or it's not at all..

Newbie

Posts

Joined
Thu Nov 18, 2021 3:10 am

Post by straightlight » Thu Dec 02, 2021 2:49 am

To update the quantity field in your PHPMyAdmin console altogether, by using a query, instead of making these changes would also work since the changes above suggests to limit to the minimum value without the use of an API which could be re-differed when the case applies.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by moshair » Thu Dec 02, 2021 7:45 pm

To limit 1 item from the controller edit:

catalog/controller/checkout/cart.php

and change

Code: Select all

$this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);

$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
to

Code: Select all

$result = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `product_id` = '" . $this->request->post['product_id']. "' AND `option` = '" . $this->db->escape(json_encode($option)) . "' ");
if ($result->num_rows) {
$json['success'] = '<span style="color:red">Product <b>' . $product_info['name'] . '</b> is in your cart!</span>';
}
else{
$this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
}
I tried it, it is working very good. I'm not very familiar with oc programming, if some one found an error in the code please write down it.
Regards,

New member

Posts

Joined
Sun Jul 21, 2019 3:27 pm

Post by straightlight » Thu Dec 02, 2021 7:48 pm

Hardcoded text, however.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: Baidu [Spider], Semrush [Bot] and 182 guests