Post by Progenix » Sat Jan 06, 2024 11:12 pm

Hello everyone, happy 2024!

I have an issue with the function cart.remove. On a product page, I want a button to add or remove a related product (think of it as buying a burger and having chips as the related product), but without it being an option on the product - just an add/remove BUTTON for a related product as I'm not a fan of the way options are handled. It adding the primary item as well as the optional extra should be two separate items in the cart/checkout/etc.

I have managed to use onclick="cart.add('123')" to add product ID 123 to the cart, but onclick="cart.remove('123')" simply refreshes the page. If I type "cart.add('123')" in the browser console it adds item 123, while "cart.remove('123')" returns "undefined."

I'm pretty certain that cart.remove('x') must be a valid function, as if I view the source for the page with an item in the cart I see the following in the source code:

<td class="text-center"><button type="button" onclick="cart.remove('123');" title="Remove" class="btn btn-danger btn-xs"><i class="fa fa-times"></i></button>

Am I missing something?

Newbie

Posts

Joined
Wed Jul 29, 2020 7:17 pm

Post by straightlight » Sat Jan 06, 2024 11:34 pm

Progenix wrote:
Sat Jan 06, 2024 11:12 pm
Hello everyone, happy 2024!

I have an issue with the function cart.remove. On a product page, I want a button to add or remove a related product (think of it as buying a burger and having chips as the related product), but without it being an option on the product - just an add/remove BUTTON for a related product as I'm not a fan of the way options are handled. It adding the primary item as well as the optional extra should be two separate items in the cart/checkout/etc.

I have managed to use onclick="cart.add('123')" to add product ID 123 to the cart, but onclick="cart.remove('123')" simply refreshes the page. If I type "cart.add('123')" in the browser console it adds item 123, while "cart.remove('123')" returns "undefined."

I'm pretty certain that cart.remove('x') must be a valid function, as if I view the source for the page with an item in the cart I see the following in the source code:

<td class="text-center"><button type="button" onclick="cart.remove('123');" title="Remove" class="btn btn-danger btn-xs"><i class="fa fa-times"></i></button>

Am I missing something?
OC version. URL.

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 Progenix » Sun Jan 07, 2024 1:02 am

straightlight wrote:
Sat Jan 06, 2024 11:34 pm
OC version. URL.
100%. OC 2.3.0.2, https://progenix.co.za

Newbie

Posts

Joined
Wed Jul 29, 2020 7:17 pm

Post by paulfeakins » Mon Jan 08, 2024 6:47 pm

Progenix wrote:
Sat Jan 06, 2024 11:12 pm
Am I missing something?
Yep, items aren't removed from the cart by their Product ID, they are given a different "key" in the cart.

If you look in your common.js file:
view-source:https://progenix.co.za/catalog/view/jav ... /common.js

Under the comment "// Cart add remove functions" you'll see the remove method that takes a key.

You'll probably need a developer to help if you want to find out which key maps to which product ID because it's not immediately obvious to me from the code how to find that.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Progenix » Mon Jan 08, 2024 8:16 pm

paulfeakins wrote:
Mon Jan 08, 2024 6:47 pm
Progenix wrote:
Sat Jan 06, 2024 11:12 pm
Am I missing something?
Yep, items aren't removed from the cart by their Product ID, they are given a different "key" in the cart.

If you look in your common.js file:
view-source:https://progenix.co.za/catalog/view/jav ... /common.js

Under the comment "// Cart add remove functions" you'll see the remove method that takes a key.

You'll probably need a developer to help if you want to find out which key maps to which product ID because it's not immediately obvious to me from the code how to find that.
Hmmh, I see that. With item ID 123 in the cart, the source shows the following:

<td class="text-center"><button type="button" onclick="cart.remove('39631');" title="Remove" class="btn btn-danger btn-xs"><i class="fa fa-times"></i></button></td>

Removing and re-adding the same item increments the ID by 1 each time. Adding a different item also increments the ID by 1.

That got me searching and I found oc_cart to contain all current items in all currnet carts with a unique ID for each.

An example of a cart that contains product IDs 123 and 39633 has the following (there are 8 column, but the important ones are here):

cart_id: 39631
product_id: 123

cart_id: 39634
product_id: 5025

Clearly not that easy :(

If I enter cart.remove('[id from DB]') in the console it still spews out "undefined" but does indeed remove the item, so now I need to figure out how to pull cart_id.

Newbie

Posts

Joined
Wed Jul 29, 2020 7:17 pm

Post by paulfeakins » Tue Jan 09, 2024 7:43 pm

Progenix wrote:
Mon Jan 08, 2024 8:16 pm
so now I need to figure out how to pull cart_id.
Have a look at the code for the little cart popup at the top right and the cart page itself.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: No registered users and 20 guests