Author Topic: How to add a product into cart non-ajax solution  (Read 3512 times)

Jurix

  • Beginner
  • *
  • Posts: 2
  • Skype Name: bebecek
How to add a product into cart non-ajax solution
« 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!

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10018
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: How to add a product into cart non-ajax solution
« Reply #1 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)
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

fredzebu

  • Beginner
  • *
  • Posts: 15
Re: How to add a product into cart non-ajax solution
« Reply #2 on: October 27, 2014, 04:46:01 am »

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?

mschratt

  • Beginner
  • *
  • Posts: 1
Re: How to add a product into cart non-ajax solution
« Reply #3 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