Read More
Menu Close

    Understanding and manipulating cart in Joomla Virtuemart

    Like every platform Joomla Virtuemart stores its cart data in the current session for any guest or registered user, and creates an entry in its database table for a registered user only. In Joomla we can get these data as –

    // get Joomla session object
    $session = JFactory::getSession();
    
    // get current session cart data
    $session->get('vmcart',0,'vm');
    
    To specify Virtuemart uses namespace ‘vm’ to store its data in Joomla session

    In reference to database Virtuemart stores cart data in ‘#__virtuemart_carts’ table, so you can get or update this data from there, with respect to a user id. Below presented is a simple query to get cart data from that table

    // get Joomla database object
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select('cartData')
    	->from($db->quoteName('#__virtuemart_carts'))
    	->where('virtuemart_user_id='.$userId);
    $db->setQuery($query);
    try{
    	$vmCart = $db->loadResult();
    }
    catch (Exception $e){
         // To show any exception if occured
         return JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
    }
    

    Data received from above query will be JSON encoded, hence you have to simple use json_decode to use this data. Though one thing to mention here is that how virtuemart perform this json_decode so that the final data will be same as virtuemart get it, so it would not create any issues afterwards in data handling.

    (object)json_decode($vmCart,true);
    

    As you can see, json_decode is used with true as second parameter here because we want returned objects to be converted into associative arrays. Furthermore, it is afterwards casted with object. To understand its requirement in brief – Virtuemart requires card data as an object but also requires its parameter cartProductsData as an array containing arrays of products.

    Structure would be as-

    stdClass Object
    (
        [cartProductsData] => Array
            (
                [0] => Array
                    (
                        [virtuemart_product_id] => 3178
                        [quantity] => 1
                        [customProductData] => Array
                            (
                            )
    
                    )
    
                [1] => Array
                    (
                        [virtuemart_product_id] => 3177
                        [quantity] => 1
                        [customProductData] => Array
                            (
                            )
    
                    )
    
            )
    
        [vendorId] => 1
        . // other parameters
        . 
        .
        . // other parameters
    )
    

    Finally you can add, manipulate or remove elements of cartProductsData array and set back the updated cart object, which is finally managed by class VirtueMartCart

    Start your headless eCommerce
    now.
    Read More
    // include class file
    if (!class_exists( 'VirtueMartCart' )) 
    	require(JPATH_SITE.DIRECTORY_SEPARATOR .'components' .DIRECTORY_SEPARATOR.'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'cart.php');
    
    // reset session for corrected session cart
    // the session will surely contain fully encoded cart data
    $session->set('vmcart', json_encode($yourNewCartData), 'vm');
    
    // get updated cart object
    $cart = VirtueMartCart::getCart();
    

    In another approach, if you are on the cart page you can simply redirect to cart, after setting cart session, so that Virtuemart will manage all the objects on its own with new values.

    $mainframe = JFactory::getApplication();
    $mainframe->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart', FALSE));
    
    Usage:

    Cart manipulation is useful in extensions like – Single Seller Checkout or Split Cart, where the cart needs to be manipulated as per particular choice. One of this kind of extension is available on Webkul store here:

    Joomla Virtuemart Marketplace Split Cart

    For any query regarding Joomla virtuemart plug-ins and add-ons you can communicate with us by creating a ticket at:
    https://webkul.uvdesk.com/en/customer/create-ticket/

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top