【问题标题】:Weird behaviour from joomla and virtuemart regarding cart datajoomla 和美德玛特关于购物车数据的奇怪行为
【发布时间】:2016-07-15 21:14:54
【问题描述】:

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

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

【问题讨论】:

    标签: php joomla virtuemart


    【解决方案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 系统

      • /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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多