joomla 和 virutemart 关于购物车数据的奇怪行为

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/includes/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';

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

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

  1. JPATH_BASE 可以像

    一样给出确切的位置
    • /var/www/store 对于 linux 系统

    • /Applications/MAMP/htdocs/store 对于 MAC

    • C:/xampp/htdocs/www/store 对于 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');