简体   繁体   English

Virtuemart 2-我怎样才能通过venuemart_product_id将商品添加到购物车会话中?

[英]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. 我的示例代码可尝试使用产品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 . 将商品添加到购物车的确切方法类似于遵循其ajax调用功能。

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. 如果要从php部分执行此操作,请转到购物车控制器文件并检查功能addJS,然后将相应的参数传递给该功能。

Hope this may help you.. 希望这对您有帮助。

Please use the VirtueMart API for correct cart handling. 请使用VirtueMart API正确处理购物车。 http://forum.virtuemart.net/index.php?topic=125870.msg431290#msg431290 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. 简而言之,默认情况下,VirtueMart使用通过jQuery.getJSON完成的调用,这是Ajax函数的简写。 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. 您应该使用VM API来检索和存储购物车对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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