VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: Jurix on September 23, 2014, 22:01:28 PM

Title: How to add a product into cart non-ajax solution
Post by: Jurix on September 23, 2014, 22:01:28 PM
Hi guys.
I found a way to add a product item into a cart using AJAX: http://stackoverflow.com/questions/14595536/virtuemart-2-how-i-can-add-item-into-cart-session-with-virtuemart-product-id It works good.

However I would need to add a product in PHP script. I think I should use a public function add() located in components/com_virtuemart/helpers/cart.php (line 350), correct?

I'm trying to call this func from my component but no positive result.

Any suggestions?
Thanks in advance!
Title: Re: How to add a product into cart non-ajax solution
Post by: Milbo on September 25, 2014, 15:04:05 PM
I am sorry, but what a nonsense.

The native function is using "ajax". Check the function in the cart controller addJS. There you see we use $view = $this->getView ('cart', 'json'); then we use echo json_encode($this->json);

The call is done with jQuery.getJSON which is a shorthand Ajax function, which is equivalent to:

   $.ajax({
      url: url,
      dataType: 'json',
      data: data,
      success: callback
    });

and yes the function for the cart is add. Furthere you should never create the cart yourself, use the API for that!

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

Then you do $cart->add(Yourstuff)
Title: Re: How to add a product into cart non-ajax solution
Post by: fredzebu on October 27, 2014, 04:46:01 AM
Quote from: Milbo on September 25, 2014, 15:04:05 PM

and yes the function for the cart is add. Further you should never create the cart yourself, use the API for that!

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

Then you do $cart->add(Yourstuff)

VirtueMart 2.6.10  and Joomla! 2.5.24

Thank you for this info. I can empty the cart:
      $cart = VirtueMartCart::getCart();
      $cart->emptyCart();

I want to add  a single product (id=77, qty=1). What form does the array 'Yourstuff' take?
Title: Re: How to add a product into cart non-ajax solution
Post by: mschratt on February 01, 2015, 14:39:53 PM
In Reply to Milbo: Many thanks for your answer. it helped me with my Problem too.

Can I add a product (configured Price e.g. 10 Euro) with a Price of 0? What is the standard way to solve this with virtuemart? Or do i have to program my own function therefore?

I know about configuring discounts for Single products but i Need this Discount for every product (products added through a separate software directly to the Database) if enough other products (Cart sum is high enough) are in the Cart...

Thanks
Markus