[英]Displaying cart total outside of joomla framework
我一直在努力解决这个问题。
我让我的joomla / virtuemart在名为“store”的目录中运行,我希望在joomla框架之外显示购物车中的产品总数。 所以我想出了这个代码,它适用于旧版本的virtuemart(<v3)。 但是,在使用Virtuemart 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;
?>
而错误:
警告:在第20行的/home/me/public_html/includes/header.php中为foreach()提供的参数无效
0
其中0代表购物车总数,应该显示为1,因为当时我在购物车中有一个商品
我不是一个PHP专家,但打开谷歌搜索,在我看来,我的$数组不是一个导致问题的数组?
这真让我感到困惑,因为它在旧版本的virtmart中运行良好。
-
鉴于下面的答案,我试图运行这个:
<?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';
}
该阵列具有所有购物车项目详细信息,只需选择您想要的任何内容。
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.