dougou8639 2016-07-15 21:14
浏览 42
已采纳

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

So I wrote up this code to display the cart total outside of joomla framework:

<?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;

?>

which works fine (by displaying the total items in cart) in a top level directory (/public_html/test.php)

but if I move it to a second-level directory, like (/public_html/includes/test.php), I get this error: first, the code (notice the /../store because we're in a second-level now):

    <?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;

?>

then the error:

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

I have no clue why it works fine in the top level but then not in subdirectories. Any ideas?

  • 写回答

1条回答 默认 最新

  • dongtan6336 2016-07-16 05:05
    关注

    When you are going to subdirectory the path changes. I got these paths for the defined JPATH_BASE. For top level

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

    And for subdirectory

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

    As dirname(realpath(FILE) will give the location of current directory it is in.

    So there are two methods to get correct path

    1. JPATH_BASE can be given the exact location like

      • /var/www/store for linux system

      • /Applications/MAMP/htdocs/store for MAC

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

    example

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

    These are just examples.

    1. It can also be achieved this way by defining JPATH_BASE this way

    Replace

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大