works on localhost but not online...
I'm using a PHP cart to apply discount coupons before connecting to PayPal for payment.
Works fine on XAMPP/localhost but not when I upload the files to client's server -
Middle two cases not working (buy one get discount = free product) and they need to work together, that's why all the session vars...
discount defaults to 0
First case ($10 off) and last (% off) work fine...
Is it something about the sessions or the variables?
Live site is here
function checkCoupon($amount) // Check coupon amount
{
global $_CONFIG, $_CART, $cc, $totamount, $discset;
if ($_CONFIG['coupon_system'] == 1 && !empty($_SESSION['COUPON'])) // Coupon system on and coupon set?
{
$sc = unserialize($_SESSION['COUPON']); // Yum!
if (!in_array($_CART['item_number'], $sc['disallow']) && $sc['expires'] > time()) // Not disallowed or expired?
{
if (strspn($sc['discount'],"$")){
$offpos = strpos($sc['discount'],"off");
$disc = substr($sc['discount'],1,$offpos-1);
$over = substr($sc['discount'],$offpos+3);
$disc = $disc;
$over = $over;
$totamount = $totamount + ($amount * $_CART['quantity']);
if (($totamount >= $over) && !$discset) { $amount = $_CART['amount'] - ($disc / $_CART['quantity']); $_ACCOUNTING['amount_discount'] = 10; $discset = 1;}
else if (($totamount >= $over) && $discset) { $amount = $_CART['amount'] - $_ACCOUNTING['amount_discount']; $_ACCOUNTING['amount_discount'] = 10;}
return($amount); // Return discounted amount
}
else if (strstr($sc['discount'],"Omega-X for Dogs") && !(isset($_SESSION['omxset']))){
if ($_SESSION['urxset']) {$urx = 19.98 / $_CART['quantity']; unset($_SESSION['urxset']);}
else $urx = 0;
$ordertot = implode($_SESSION['shopping']);
if (strstr($ordertot,"Osteo-X") && strstr($ordertot,"Omega-X for Dogs")){
$_SESSION['omxset'] = 1;
$omx = 24.98 / $_CART['quantity'];
}else $omx = 0;
$amount = $_CART['amount'] - (($omx + $urx) / $_CART['quantity']);
$_ACCOUNTING['amount_discount'] = (($omx + $urx) / $_CART['quantity']);
return($amount); // Return discounted amount
}
else if (strstr($sc['discount'],"Omega-X for Cats") && !(isset($_SESSION['urxset']))){
if ($_SESSION['omxset']) {$omx = 24.98 / $_CART['quantity']; unset($_SESSION['omxset']);}
else $omx = 0;
$ordertot = implode($_SESSION['shopping']);
if (strstr($ordertot,"Uro-X") && strstr($ordertot,"Omega-X for Cats")){
$_SESSION['urxset'] = 1;
$urx = 19.98 / $_CART['quantity'];
}else $urx = 0;
$amount = $_CART['amount'] - (($urx + $omx) / $_CART['quantity']);
$_ACCOUNTING['amount_discount'] = (($urx + $omx) / $_CART['quantity']);
return($amount); // Return discounted amount
}
else{
$amount = ($amount - $amount / 100 * $sc['discount']); // Discounted amount
return($amount); // Return discounted amount
}
}
}
//if no coupons match
return($amount); // Return amount with no discount
}
decibel.places posted this at 16:24 — 21st October 2008.
He has: 1,494 posts
Joined: Jun 2008
The problem is the $_SESSION variables
For some reason, they don't get destroyed at the end of the session, so they stick around.
Actually, this is desireable, to prevent multiple uses of the coupons by the same user/client.
I can unset them, and the coupon works, but I can't figure out how to unset them only once, so the combo use doesn't work.
But why would XAMPP let them go away and client's server wouldn't?
(I do not know config of client's server, I am only working with ftp).
pr0gr4mm3r posted this at 17:21 — 21st October 2008.
He has: 1,502 posts
Joined: Sep 2006
The server probably keeps the session active longer than your local setup.
decibel.places posted this at 01:06 — 22nd October 2008.
He has: 1,494 posts
Joined: Jun 2008
yes, that is my conclusion
actually, the longer session life prevents reuse of the coupons, which is desireable
but makes testing more complex, I got proficient at clearing saved data (cookies etc) in Firefox and Opera...
decibel.places posted this at 05:28 — 26th October 2008.
He has: 1,494 posts
Joined: Jun 2008
I have been banging my head on the keyboard for the past few days.
I can't seem to get it right.
Works on XAMPP/localhost also in Safari and Chrome.
But in IE and Firefox, the $_SESSION variable gets set without applying the discount.
If I unset the $_SESSION variables on each page load, then the discount will apply to one coupon at a time, but not both (of course). Client is cool with that, for now.
It doesn't appear to be persistent sessions because the page will load without the $_SESSION variables set, then when the coupon is applied the $_SESSION variable is set without applying the coupon...
Maybe I will set up a database as an alternative, but this "should" work, and does in some environments but not others!
JeevesBond posted this at 23:20 — 28th October 2008.
He has: 3,956 posts
Joined: Jun 2002
I too have wasted an inordinate amount of time on $_SESSION recently. So don't worry, you're not alone.
I don't understand why this would work in some browsers and not others, that makes no sense. Unless they're not storing the session id correctly, but you're intimating that the problem is happening while processing a single page (?).
Perhaps the best answer is to use
$_SESSION
sparingly, keeping values in variables and only saving what you really need toward the end of the script.a Padded Cell our articles site!
decibel.places posted this at 01:15 — 29th October 2008.
He has: 1,494 posts
Joined: Jun 2008
Well, this is a script I adapted, a nice front-end for stardard PayPal payments.
It has its own session vars and cookies, which do seem to go away at the end of the transaction. I tried adding my vars to its session arrays, that didn't work either.
Since there is no database, and I need to preserve one discount while processing another coupon on a fresh page load, I think I have no option but session (hmmm - just had an idea, maybe I can set a cookie!)
Anyway, despite my woes, it is a good app and I would attach it because it is very hard to find, the creator's web site has disappeared - but you don't permit zip attachments. So here is a download link.
The default distro only has support for % off discounts, but I added $ off with qualifying amount, and buy one item get another free. If you need some help with those, PM me.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.