VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: aqr996 on May 31, 2016, 01:38:44 AM

Title: Displaying cart total outside of joomla framework
Post by: aqr996 on May 31, 2016, 01:38:44 AM
Hi,
I've been struggling with this. I've got my joomla/virtuemart running in a directory called "store" and I wish to display the total number of products in the cart outside of the joomla framework. So I've come up with this code, which works fine with older versions of virtuemart ( < v3). However, when trying it with Virtuemart 3.0.16, I'm running into these problems:

The code (which is in a directory called "includes")

Quote<?php

// Set flag that this is a parent file

define( '_JEXEC', 1 );

define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );

define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');

$mainframe = JFactory::getApplication('site');

    $array = unserialize($_SESSION['__vm']['vmcart']);
    $total = 0;
    foreach($array->products as $product){
        $total += $product->amount;
    }
    echo "" . $total;
?>

And the error:

Quote
Warning: Invalid argument supplied for foreach() in /home/me/public_html/includes/header.php on line 20
0
where 0 represents the cart total, which should show 1, since at the time I had an item in cart

I'm not a php expert, but open googling that, it seems to me that my $array isn't an array which is causing the problems?

This is really confusing me since it's working fine in older version of virtuemart.
Title: Re: Displaying cart total outside of joomla framework
Post by: Milbo on May 31, 2016, 10:51:52 AM
I think the changes are joomla3 and not VM
Title: Re: Displaying cart total outside of joomla framework
Post by: Studio 42 on May 31, 2016, 11:19:28 AM
Add same code as the cart module :
<?php

// Set flag that this is a parent file

define'_JEXEC');

define('JPATH_BASE'dirname(realpath(__FILE__)). '/../store' );

define'DS'DIRECTORY_SEPARATOR );

require_once ( 
JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( 
JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
// NEW CODE
if (!class_exists'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!
class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart VirtueMartCart::getCart(false);
$data $cart->prepareAjaxData();
foreach (
$data->products as $product){
// here what you need to display see \modules\mod_virtuemart_cart\tmpl\default.php
}
Title: Re: Displaying cart total outside of joomla framework
Post by: aqr996 on May 31, 2016, 13:00:44 PM
Thanks, I tried this but I got the following new errors:

Quoterequire() @ /home/me/public_html/includes/test.php:16
JFactory :: getApplication() @ /home/me/public_html/store/administrator/components/com_virtuemart/helpers/config.php:25
JError :: raiseError() @ /home/me/public_html/store/libraries/joomla/factory.php:99
JError :: raise() @ /home/me/public_html/store/libraries/joomla/error/error.php:251
JError :: throwError() @ /home/me/public_html/store/libraries/joomla/error/error.php:176
call_user_func_array() @ /home/me/public_html/store/libraries/joomla/error/error.php:214
JError :: handleCallback()
call_user_func() @ /home/me/public_html/store/libraries/joomla/error/error.php:765
JError :: customErrorPage()
JFactory :: getApplication() @ /home/me/public_html/store/libraries/joomla/error/error.php:784
JError :: raiseError() @ /home/me/public_html/store/libraries/joomla/factory.php:99
JError :: raise() @ /home/me/public_html/store/libraries/joomla/error/error.php:251

No clue what they mean.
Title: Re: Displaying cart total outside of joomla framework
Post by: Studio 42 on May 31, 2016, 16:29:37 PM
require() @ /home/me/public_html/includes/test.php:16
error line 16?
Please copy your current code, i cannot know your line 16 content.
But i think you can remove this 2 lines:
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');

and try to load site if it not work using :
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();