VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: fredzebu on November 02, 2014, 02:16:22 AM

Title: php add product to cart
Post by: fredzebu on November 02, 2014, 02:16:22 AM
VirtueMart 2.6.10  and Joomla! 2.5.24 and php5.4

I want to be able to add a product to the cart. I can empty the cart if required:

if (!class_exists('VirtueMartCart'))  require(VMPATH_SITE . DS . 'helpers' . DS . 'cart.php');
$cart->emptyCart();

Now I believe I need to do:

$cart->add(Yourstuff);

but I am unsure what the data should look like in the array 'Yourstuff'
eg. I want to add  a single product (id=77, qty=1). What form does the array 'Yourstuff' take?

Any help will be greatly appreciated.
Title: Re: php add product to cart
Post by: jenkinhill on November 02, 2014, 15:07:30 PM
Look at this example:  http://forum.virtuemart.net/index.php?topic=94539.msg337937#msg337937

or  https://www.spiralscripts.co.uk/joomla-plugins/vm2-product-snapshot-detail
Title: Re: php add product to cart
Post by: fredzebu on November 02, 2014, 20:32:40 PM
Hi Jenkinhill, Thank you for your reply.
Neither solution you have suggested will fit my requirements. They both create a link for the user to click "Add to cart".
eg  option=com_virtuemart&view=cart&task=add&quantity[]=1&virtuemart_product_id[]=77&virtuemart_category_id[]=1

I want to add the product (or multiple products) directly as a result of an event that has already occurred.
Hence the need to prepare the array "Yourstuff" to execute:

$cart->add(Yourstuff);
Title: Re: php add product to cart
Post by: fredzebu on November 06, 2014, 07:03:37 AM
Ok I have worked it out myself. I can now replace one product with another product when the required event has occurred:

  $cart = VirtueMartCart::getCart();
  $produst_ids=array();
  $product_ids[0]=77;
  $success = true;
  $cart->add($product_ids,$success);

  $cart->updateProductCart(76,0);

Hope this helps someone else with a similar need.