Joomla和Virtuemart关于购物车数据的奇怪行为


Weird behaviour from joomla and virtuemart regarding cart data

所以我写了这段代码来显示joomla框架之外的购物车总数:

<?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'); 
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
  $total += $product[quantity];
}
echo $total;
?>

在顶级目录 (/public_html/test.php) 中工作正常(通过显示购物车中的总项目)

但是如果我将其移动到二级目录,例如(/public_html/include/test.php),则会出现此错误:首先,代码(注意/../store,因为我们现在处于第二级):

    <?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'); 
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
  $total += $product[quantity];
}
echo $total;
?>

然后错误:

Fatal error: Uncaught exception 'Exception' with message 'XML file did not load' in /home/me/public_html/store/libraries/joomla/form/form.php:2020 Stack trace: #0 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmplugin.php(201): JForm::getInstance('weight_countrie...', '/home/me/publ...', Array, false, '//vmconfig | //...') #1 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmpsplugin.php(45): vmPlugin::getVarsToPushByXML('/home/me/publ...', 'weight_countrie...') #2 /home/me/public_html/store/plugins/vmshipment/weight_countries/weight_countries.php(44): vmPSPlugin->getVarsToPush() #3 /home/me/public_html/store/libraries/joomla/plugin/helper.php(194): plgVmShipmentWeight_countries->__construct(Object(JDispatcher), Array) #4 /home/me/public_html/store/libraries/joomla/plugin/helper.php(125): JPluginHelper::_import(Object(stdClass), true, NULL) #5 /home/me/public_html/store/administrator/components/com_virtuemart/helpe in /home/me/public_html/store/libraries/joomla/form/form.php on line 2020

我不知道为什么它在顶层工作正常,但在子目录中却不能。有什么想法吗?

当您要去子目录时,路径会发生变化。我得到了定义的JPATH_BASE的这些路径.对于顶级

string '/Applications/MAMP/htdocs/store';

对于子目录

string '/Applications/MAMP/htdocs/test/../store';

作为 dirname(realpath(FILE) 将给出它所在的当前目录的位置。

所以有两种方法可以获得正确的路径

  1. JPATH_BASE可以给出确切的位置,例如

      /
    • var/www/store for Linux system

    • /Applications/MAMP/htdocs/store for MAC

    • C:/xampp/htdocs/www/store for windows

define('JPATH_BASE', '/var/www/store' );

这些只是例子。

  1. 也可以通过这种方式定义JPATH_BASE来实现

取代

define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );

$path=getcwd();
$parts = explode(DIRECTORY_SEPARATOR, $path);
$pop = array_pop($parts);
$path = implode(DIRECTORY_SEPARATOR, $parts);
define('JPATH_BASE', $path.'/store');