[英]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. 我让我的joomla / virtuemart在名为“store”的目录中运行,我希望在joomla框架之外显示购物车中的产品总数。 So I've come up with this code, which works fine with older versions of virtuemart ( < v3).
所以我想出了这个代码,它适用于旧版本的virtuemart(<v3)。 However, when trying it with Virtuemart 3.0.16, I'm running into these problems:
但是,在使用Virtuemart 3.0.16进行尝试时,我遇到了以下问题:
The code (which is in a directory called "includes") 代码(位于名为“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
警告:在第20行的/home/me/public_html/includes/header.php中为foreach()提供的参数无效
0
0
where 0 represents the cart total, which should show 1, since at the time I had an item in cart 其中0代表购物车总数,应该显示为1,因为当时我在购物车中有一个商品
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? 我不是一个PHP专家,但打开谷歌搜索,在我看来,我的$数组不是一个导致问题的数组?
This is really confusing me since it's working fine in older version of virtuemart. 这真让我感到困惑,因为它在旧版本的virtmart中运行良好。
-- -
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
它不断说0
Try this, 尝试这个,
after loading Joomla framework just use below codes it should work 加载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';
}
The array have all the cart item details just choose what ever you want. 该阵列具有所有购物车项目详细信息,只需选择您想要的任何内容。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.