我要尝试的示例代码,使用产品 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