[英]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.