News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

php add product to cart

Started by fredzebu, November 02, 2014, 02:16:22 AM

Previous topic - Next topic

fredzebu

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.

jenkinhill

Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum

fredzebu

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);

fredzebu

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.