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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么