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

时间:2016-07-15 21:14:55

标签: php joomla virtuemart

所以我编写了这段代码来显示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

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

1 个答案:

答案 0 :(得分:1)

当你要去子目录时,路径会发生变化。我为定义的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

    • /应用程序/ MAMP / htdocs / store for MAC

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

  2. 示例

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

    这些只是一些例子。

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

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

      BY

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