简体   繁体   中英

Virtuemart 2 - How i can add item into cart session with virtuemart_product_id?

My Sample Code to try, automatic add to cart with Product ID. I know i need the quanitity too, but i dont know exactly the code. With this code it doesnt work.

$array = unserialize($_SESSION['__vm']['vmcart']);   //read the cart session
$products = $array->products;                        //list the products 

if (array_key_exists('53', $products)) {             //if productID 53 then
  $cart = VirtueMartCart::getCart();
  $quantityPost= 1;                                  //set quantity on 1
  $virtuemart_product_id = 1;                        //set product id on 1
  $cart->add(array(1));                              //add into cart product with id 1
  //$tmpProduct = $this->getProduct((int) $virtuemart_product_id,$quantityPost); //?
  echo "Article added!"; }                           //echo Message
else { 
  echo "Nothing added!"; }                           //echo Message

The exact way of adding item to the Cart is like follows its an ajax call function .

var data = "quantity[]="+qty+"&virtuemart_product_id[]="+pid
    //data      =   encodeURIComponent(data);
    jQuery.ajax({               

                    type: "GET",
                                    dataType: 'json',
                    url: "index.php?option=com_virtuemart&nosef=1&view=cart&task=addJS",
                    data: data,
                    success: function(data) {
                        alert(data);
                    }
         });

If you want to do it from php section go to the cart controller file and check function addJS then pass the corresponding param to that function.

Hope this may help you..

Please use the VirtueMart API for correct cart handling. http://forum.virtuemart.net/index.php?topic=125870.msg431290#msg431290

In short, VirtueMart uses by default a call done with jQuery.getJSON which is a shorthand for an Ajax function. So all this above is not necessary. In doubt it is also unsecure. You should use the VM API to retrieve and store the cart object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM