简体   繁体   中英

Displaying cart total outside of joomla framework

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")

<?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:

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.

--

Given the answer below, I am trying to run this:

<?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');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

     if (!class_exists( 'VmConfig' ))
     require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');

     if(!class_exists('VirtueMartCart'))
     require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
echo sizeof($cart->products);
    ?>

...but without success. It continually says 0

Try this,

after loading Joomla framework just use below codes it should work

     defined('DS') or define('DS', DIRECTORY_SEPARATOR);

     if (!class_exists( 'VmConfig' ))
     require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');

     if(!class_exists('VirtueMartCart'))
     require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');

        $cart = VirtueMartCart::getCart();
        if(sizeof($cart->products) > 0){
          echo "<pre/>";
          print_r($cart);
        }
        else{
         echo 'Your cart is empty';
        }

The array have all the cart item details just choose what ever you want.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM