Forum

Thread tagged as: Question, Docs, Shop

Delete Cart item using ajax

Hi there,

Is there any chance to delete an item from the cart using ajax? I can see in cart view i have the product ID however there is no perch function that allow me to delete that single item from my cart.

Any help is appreciated.

Thanks, Vince.

Vincenzo Alaia

Vincenzo Alaia 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use perch_shop_remove_from_cart(123)

Thanks Drew,

I could not find that function.

Vince.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you getting an error?

No all good, it works.

Thanks, Vince.

Drew McLellan said:

Are you getting an error?

Actually Drew i noticed something strange...

Now i have in my cart.php page

if(isset($_REQUEST['idProduct'])){
   perch_shop_remove_from_cart(idProduct);
}
perch_shop_cart();

i call this page via ajax passing the idProduct as parameter and append the new result that i get from perch_shop_cart();

It seems working indeed i can see that when i append the new cart i cannot see the product deleted anymore. However if i try to add the same product which i deleted before it will be added twice in the cart.

Any thoughts?

Thanks.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Presuming that $_REQUEST['idProduct'] is accurate for your context, you'll want to use:

if(isset($_REQUEST['idProduct'])){ 
    perch_shop_remove_from_cart($_REQUEST['idProduct']); 
}