I’m in the middle of setting up a ShopX cart and have the products working well and a product catalog page that works. Now, I’m trying to get the "Add to Cart" feature to work, and have included the ShopXCart function working. However, I’m not convinced the ShopXSession plugin is working, because the session data never actually seems to change.
Here’s the ShopXCart call that appears on the product detail page:
[!ShopXCart? &checkout=`4029` &debug=`GET` !]
And here’s the current version of the ShopXSession that I’m using. I’ve made a couple of alterations, but just to help me debug that it’s actually working:
<?php
// ShopXSession - plugin for MODx 0.9.x ShopX
// released to the Public Domain - use as you like.
// sets SESSION variables from the POST or GET as submitted
// by the various forms of shopX
// To Do: validate the input!
// System Events - OnWebPageInit
$e = &$modx->Event;
switch ($e->name) {
case "OnWebPageInit":
setPlaceholder('sessionDebug', 'In the ShopXSession plugin.');
$_GET['sdebug'] = 'In the ShopXSession plugin.');
// clear the cart
if (isset($_GET['a']) && $_GET['a'] == 'clear') {
unset($_SESSION['taxable']);
unset($_SESSION['orders']);
unset($_SESSION['order']);
unset($_SESSION['tokens']);
$modx->sendRedirect($modx->makeURL($modx->documentIdentifier));
}
// check for refresh or back button with token array
$token = is_numeric($_POST['token'])?$_POST['token']:'';
if (sizeof($_SESSION['tokens']) == 0 || !in_array($token, $_SESSION['tokens'], true)) {
// get the Add to Cart values
if(isset($_POST['add'])) {
$itemid = $_POST['itemid'];
$name = $_POST['name'];
$quantity = $_POST['quantity'];
$price = ereg_replace("[^0-9.]","",$_POST['shopItemPrice']);
// add any custom values for each item here
// add up quantities if repeat addition
if(is_array($_SESSION['orders'][$itemid])) {
$quantity += $_SESSION['orders'][$itemid]['quantity'];
}
// create an array of valid POST values
$line = array(
"itemid"=>$itemid,
"name"=>$name,
"quantity"=>$quantity,
"price"=>$price,
// add any custom values for each item here
);
// load values into the session
$_SESSION['orders'][$itemid] = $line;
// add to non-repeating token array
$_SESSION['tokens'][] = $token;
}
// adjust SESSION values if checkout cart is modifed
if(isset($_POST['scEditCart'])) {
foreach($_POST['scItemQuantity'] as $key=>$value) {
if($value == 0) {
unset($_SESSION['orders'][$key]);
} else {
$_SESSION['orders'][$key]['quantity'] = $value;
}
}
}
} // end if not repeating token
// load the tax values
if(!isset($_SESSION['taxable'])) {
$modx->runSnippet('ShopXTax');
}
// get the Shipping values - customize here if necessary to match a shipping option
if(isset($_POST['shipping'])){
$shipmethod = $_POST['shipmethod'];
$values = explode('||',$_POST[$shipmethod]);
$_SESSION['order']['shipmethod'] = $shipmethod;
$_SESSION['order']['calcmethod'] = $values[0];
$_SESSION['order']['calcamount'] = $values[1];
}
// get the payment option
if(isset($_POST['paychoice'])) {
$paymethod = $_POST['paymethod'];
$_SESSION['order']['paymethod'] = $paymethod;
}
// load user data from payment modules
// include external php file, using conditionals to determine which module to load
// for example, to get user data for ECHO credit card processing
//if($_SESSION['order']['paymethod'] == 'echo' && isset($_POST['some_value)) {
//include "assets/snippets/ShopX/payments/echo.php";
//}
exit;
break;
default:
return;
}
?>
So, I haven’t changed any functionality. I’ve wrapped it up like the ModX docs show for their example plugins, and I’ve tried setting a placeholder just to verify whether it’s working or not. The placeholder never displays any data, so I have to assume that it’s not working.
I’m a decent PHP programmer so I’m not afraid of getting my hands dirty in the code, but I feel like I’m missing something simple here. Oh, and I do have that plugin set to fire on OnWebPageInit, and it is active, though I’ve locked it so that only the administrator can change it.
Any ideas would be great since I need to get this website up and running!
Thanks,
Lonnie