aqibjr1
aqibjr1

Reputation: 594

Weird behaviour from joomla and virtuemart regarding cart data

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?

Upvotes: 1

Views: 97

Answers (1)

Amit Ray
Amit Ray

Reputation: 3485

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

Upvotes: 1

Related Questions