VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: DaLi on December 30, 2014, 09:58:48 AM

Title: Remove a single product from the cart in shopping cart module
Post by: DaLi on December 30, 2014, 09:58:48 AM
Hi,

I am struggling to create the function "remove a product from the cart" in the mod_virtuemart_cart module. With other words, I would like to delete a single product from my shopping cart module which is shown in the sidebar.

This function is already available in virtuemart (via delete product from cart overview) and I aim to reuse this function, however I have some problems with the php includes which will allow me to call the function.

The function is found in "components\com_virtuemart\helpers\cart.php", function:

public function removeProductCart($prod_id=0){
<<Code
}

Question:
Which files / code do I include which will enable me to call the "removeProductCart" function  from  \modules\mod_virtuemart_cart\tmpl\default.php

Specs:
Joomla! 2.5.27
VirtueMart 2.6.12.2

Thanks in advance,


Title: Re: Remove a single product from the cart in shopping cart module
Post by: Milbo on January 15, 2015, 22:56:42 PM
use the cart itself


if (!class_exists('VirtueMartCart')) require(VMPATH_SITE . DS . 'helpers' . DS . 'cart.php');
$cart = VirtueMartCart::getCart();
$cart->removeProductCart($yourId);

refresh your cart module by ajax or reload and thats it. Btw, it would be nice if you share the code and we can add it to the core

Title: Re: Remove a single product from the cart in shopping cart module
Post by: fmvm on February 23, 2015, 10:54:04 AM
I try this code and reload page but nothing happens. For example:
if (!class_exists('VirtueMartCart')) require(VMPATH_SITE . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'cart.php');
$cart = VirtueMartCart::getCart();
$yourId = 3;
$cart->removeProductCart($yourId);


Joomla 3.3.6, VM 3.0.3
Title: Re: Remove a single product from the cart in shopping cart module
Post by: Milbo on February 26, 2015, 14:57:33 PM
in vm3, I think you need an array
$yourId[] = 3;
Title: Re: Remove a single product from the cart in shopping cart module
Post by: fmvm on February 27, 2015, 08:45:45 AM
If I write:
if (!class_exists('VirtueMartCart')) require(VMPATH_SITE . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'cart.php');
$cart = VirtueMartCart::getCart();
$yourId[] = 3;
$cart->removeProductCart($yourId);


I get the warning: Illegal offset type in unset in /***/components/com_virtuemart/helpers/cart.php on line 673
unset($this->products[$prod_id]);
and ... on line 674
if(isset($this->cartProductsData[$prod_id])){

Can you write the full code? Thanks
Title: Re: Remove a single product from the cart in shopping cart module
Post by: Kuubs on November 12, 2020, 12:21:23 PM
Quote from: fmvm on February 27, 2015, 08:45:45 AM
If I write:
if (!class_exists('VirtueMartCart')) require(VMPATH_SITE . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'cart.php');
$cart = VirtueMartCart::getCart();
$yourId[] = 3;
$cart->removeProductCart($yourId);


I get the warning: Illegal offset type in unset in /***/components/com_virtuemart/helpers/cart.php on line 673
unset($this->products[$prod_id]);
and ... on line 674
if(isset($this->cartProductsData[$prod_id])){

Can you write the full code? Thanks

Is there any update for this?
Title: Re: Remove a single product from the cart in shopping cart module
Post by: roman5527 on July 21, 2022, 16:56:15 PM
Hello, How did you finally solve it? Could you share the code? well thank you
Title: Re: Remove a single product from the cart in shopping cart module
Post by: Milbo on December 10, 2024, 15:53:06 PM
Heyho, someelse pointed me to this this question. There is no cool method to remove a product. The bigger problem is, that you cannot delete a product by id, because a cart can have the same product with different selected variants. So you can only delete products by position in the list.

so lets assume you have three products in the cart and you want to delete the cart in the middle, then set in the Request (important you must use vRequest::setVar!), the old quantities and the one to delete to zero

vRequest::setVar('quantity', array(3,0,1)); then the first product gets quantity 3, the one in the middle 0 means will be deleted, and the last product with quantity one.

I did not test that yet, but it is kind of this. Check the function updateProductCart in the cart helper.