显示joomla框架外的购物车总数



我一直在努力解决这个问题。

我的joomla/virtuemart运行在一个名为"store"的目录中,我希望显示joomla框架之外的购物车中的产品总数。因此,我提出了这段代码,它可以很好地与旧版本的virtualmart(<v3)配合使用。然而,在使用Virtuelmart 3.0.16进行尝试时,我遇到了以下问题:

代码(位于名为"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;
?>

错误:

警告:中为foreach()提供的参数无效/第20行上的home/me/public_html/included/header.php

0

其中0表示购物车总数,应该显示1,因为当时我在购物车中有一个项目

我不是php专家,但打开谷歌搜索,在我看来,我的$array不是一个导致问题的数组?

这真的让我很困惑,因为它在旧版本的virtualmart中运行良好。

--

给出下面的答案,我正在尝试运行这个:

<?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);
?>

但没有成功。它不断地说0

试试这个,

在加载Joomla框架后,只需使用以下代码,它应该可以工作

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';
}

该数组具有所有购物车项目的详细信息,只需选择您想要的内容即可。

希望能有所帮助。

最新更新