如何从mod_virtuemat_cart中删除单个产品


How to remove a single product from mod_virtuemart_cart

我通过链接找到了解决方案http://forum.virtuemart.net/index.php?topic=127483.0来自Virtuelmart项目负责人:

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

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

同时$cart->emptyArt()工作于

Joomla 3.3.6,VM 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);
}