我的示例代码要尝试,使用产品ID自动添加到购物车。我知道我也需要数量,但我不确切知道代码。使用此代码它不起作用。
$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
将项目添加到购物车的确切方法如下 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);
}
});
如果你想从php部分做到这一点,转到购物车控制器文件并检查函数addJS,然后将相应的参数传递给该函数。
希望这对您有所帮助。
请使用美德市场API进行正确的购物车处理。 http://forum.virtuemart.net/index.php?topic=125870.msg431290#msg431290
简而言之,VirtueMart默认使用jQuery.getJSON完成的调用,这是Ajax函数的简写。所以以上这一切都没有必要。毫无疑问,它也是不安全的。应使用 VM API 检索和存储购物车对象。