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

How to add a product into cart non-ajax solution

Started by Jurix, September 23, 2014, 22:01:28 PM

Previous topic - Next topic

Jurix

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

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)
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

fredzebu

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?

mschratt

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