如何从 mod_virtuemart_cart 中删除单个产品

How to remove a single product from mod_virtuemart_cart

我已经通过 Virtuemart 项目负责人的 link http://forum.virtuemart.net/index.php?topic=127483.0 找到了解决方案:

if (!class_exists('VirtueMartCart')) require(VMPATH_SITE . DS . 'helpers' . DS . 'cart.php');
$cart = VirtueMartCart::getCart();
$cart->removeProductCart($yourId);

但它不起作用。我试图用 DIRECTORY_SEPARATOR 替换 DS 因为我使用 Joomla 3.x 但没有任何改变

同时 $cart->emptyCart() 起作用

Joomla 3.3.6,虚拟机 3.0.3

这是我的解决方案

function removeProductFromCart($product_id_to_remove){
    $cart = json_decode($_SESSION['__vm']['vmcart']);
    foreach($cart->cartProductsData as $k => $v){
        if($v->virtuemart_product_id == $product_id_to_remove) unset($cart->cartProductsData[$k]);
    }
    $_SESSION['__vm']['vmcart'] = json_encode($cart);
}