php - Weird behaviour from joomla and virtuemart regarding cart data -
so wrote code display cart total outside of joomla framework:
<?php // set flag 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 $product){ $total += $product[quantity]; } echo $total; ?>
which works fine (by displaying total items in cart) in top level directory (/public_html/test.php)
but if move second-level directory, (/public_html/includes/test.php), error: first, code (notice /../store because we're in second-level now):
<?php // set flag 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 $product){ $total += $product[quantity]; } echo $total; ?>
then error:
fatal error: uncaught exception 'exception' 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 works fine in top level not in subdirectories. ideas?
when going subdirectory path changes. got these paths defined jpath_base
. top level
string '/applications/mamp/htdocs/store';
and subdirectory
string '/applications/mamp/htdocs/test/../store';
as dirname(realpath(file) give location of current directory in.
so there 2 methods correct path
jpath_base can given exact location
/var/www/store linux system
/applications/mamp/htdocs/store mac
c:/xampp/htdocs/www/store windows
example
define('jpath_base', '/var/www/store' );
these examples.
- it can achieved way defining jpath_base 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');
Comments
Post a Comment