stublackett Posted June 12, 2008 Share Posted June 12, 2008 Hi, I've made a shop front for my website : http://www.toquesnuff.com/ My customer has asked for a facility which deducts 10% from the total if a correct Voucher Code is entered, He would like that code to be TQ01, TQ02 etc, All the way to 9999 I have the discount working, But it is just not changing the PHP Session totalprice, When a correct discount code is entered, Any help / ideas ?? I basically need the $_SESSION['total_price']; to collate to my $newtotal so if someone can help me with that, Would be appreciated Check For Discount <?php //Check for discount $amount = $_SESSION['total_price'];; $discount = $_GET['value']; if ($discount == "TQ") { $total = $value; $discount = 0.9; $newtotal = $amount * 0.9; echo $newtotal; } ?> Shopping Cart Page <? $_SESSION['subtotal_price'] = number_format(($net_price + $shipping_price), 2); $_SESSION['vat_price'] = number_format(((($net_price + $shipping_price) * 1.175) - ($net_price + $shipping_price)), 2); $_SESSION['total_price'] = number_format((($net_price + $shipping_price) * 1.175), 2); if($_SESSION['shipping'] !=="WO") $price = $_SESSION['total_price']; else $price = $_SESSION['subtotal_price']; $cont = fread($handle,320); echo $cont; $convert_array = explode("\n",$cont); foreach($convert_array as $key=>$value) { if(strlen($value) > 16) { ?> <option> <? $arr = explode("\t",$value); echo $arr[0]; ?> <? echo number_format(floor($price*($arr[1]/100000))/100,2); ?> </option> <? } } ?> </select> <br /> <span class="style2">(This is an approximate conversion, your actual charge will be decided by your banks local exchange rate)</span>--> <? } else { } ?> </div> <div id="cart_info_right"> <? if( ($_SESSION['shipping'] !== "WO") && ($_SESSION['shipping'] !== "TF")) $total_overall = $_SESSION['total_price']; else $total_overall =$_SESSION['subtotal_price'];?> <div align="left"> <?php $_SESSION['subtotal_price'] = number_format(($net_price + $shipping_price), 2); $_SESSION['vat_price'] = number_format(((($net_price + $shipping_price) * 1.175) - ($net_price + $shipping_price)), 2); $_SESSION['total_price'] = number_format((($net_price + $shipping_price) * 1.175), 2); ?> <div style="float:right"> <script type="text/javascript"> AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','200','height','150','title','Currency Conversion','src','convert','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','flashvars','TotalPrice=<?=($total_overall); ?>','wmode','transparent','movie','convert' ); //end AC code </script> <noscript> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="200" height="150" title="Currency Conversion"> <param name="movie" value="convert.swf" /> <param name="quality" value="high" /> <param name="flashVars" value="TotalPrice=<?=($total_overall); ?>" /> <param name="wmode" value="transparent" /> <embed src="convert.swf" width="200" height="150" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" flashvars="TotalPrice=<?=$total_overall ?>" wmode="transparent"></embed> </object> </noscript></div> <div id="price_container"> <div id="shipping_price"> <h3>Shipping: <?=$shipping_display ?></h3> </div> <div id="net_price"> <h3>Subtotal: £ <?=$_SESSION['subtotal_price'] ?> </h3> </div> <? if( ($_SESSION['shipping'] !== "WO") && ($_SESSION['shipping'] !== "TF")) { ?> <div id="vat_price"> <h3>VAT: £<?= $_SESSION['vat_price'] ?></h3> </div> <div id="discount"> <h3>Discount Code : </h3> <form name="discount" method="get" /> <p> <input type="text" name="value" /> <span class="style5">Enter for a 10% discount</span> </p> <p> <input name="submit" type="submit" title="submit" value="Update Order" /> </p> <p> </p> <p></form> </p> </div> <div id="total_price"> <h1>Total: £<?=$_SESSION['total_price']; ?></h1> </div> <? } else { ?> <div id="total_price"> <h1>Total: £<?=$_SESSION['subtotal_price'] ?></h1> </div> <? } echo ' </div> <div class="float_clear"></div> '; ?> <div id="cart_info_middle" style="z-index:-1"> Deliver to: <form action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>" method="post" name="choosedelivery" target="_self" id="choosedelivery" style="display: inline;"> <select name="delivery" class="delivery" onChange="submit()" style="font-size:10px; height:20px;"> <?php if(isset($_SESSION['shipping'])) { if($_SESSION['shipping'] == "UK"){ echo "<option value=\"UK\"><strong>United Kingdom</strong></option>\n"; } if($_SESSION['shipping'] == "EU"){ echo "<option value=\"EU\"><strong>Rest of Europe</strong></option>\n"; } if($_SESSION['shipping'] == "WO"){ echo "<option value=\"WO\"><strong>Rest of World</strong></option>\n"; } } if($_SESSION['shipping'] !== "UK"){ echo "<option value=\"UK\">United Kingdom</option>\n"; } if($_SESSION['shipping'] !== "EU"){ echo "<option value=\"EU\">Rest of Europe</option>\n"; } if($_SESSION['shipping'] !== "WO"){ echo "<option value=\"WO\">Rest of World</option>\n"; } ?> </select> <? if($_SESSION['shipping'] == "TF"){ echo "<br /><b> You will have to enter your VAT number on the next screen</b>"; } ?> </form> <br /> <? if(($net_price + $shipping_price) >= 100) { ?><div>Buying bulk? Want to sell Toque Snuff through your own Business? <a href="contact.php">Contact us</a></div><? } ?> <? if(($net_price + $shipping_price) >= 5) { ?> <b> By pressing Checkout you are verifying you are over 18 years of age and agree to the <a href="terms.php" onclick="return popupterms()">terms and conditions</a></b> <a href="checkout_convert.php" title="Checkout" style="border:0px solid black" ><h1>Checkout</h1></a> <? } else { ?><span style="color:#FF0000"><b>Minimum Order £5.00 not reached</b></span> <? } ?> </div> </div> <div class="float_clear"> </div> </div> <div class="float_clear"></div> <?php } ?> </div></div> <div align="right"><span class="secure"><a href="http://www.mastercardbusiness.com/mcbiz/index.jsp?template=/orphans&content=securecodepopup" target="_blank" onclick="popup('http://www.mastercardbusiness.com/mcbiz/index.jsp?template=/orphans&content=securecodepopup'); return false" ><img src="images/MCSC.gif" alt="Mastercard Secure Code" width="80" height="44" border="0" /></a><img src="images/VBV.gif" alt="Verified by Visa" width="100" height="41" /></span> </div> <? } ?> <!-- View Cart.php Code ENDS here --> <!-- New Search Code Begins --> <? if(@mysql_num_rows($search_results) > 0) { ?> <h1 align="center"><?= $_GET['search']; ?></h1> <? while($row = mysql_fetch_assoc($search_results)) { $type =0; ?> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="result"> <tr> <? if($type == 0) imageOut($row); else detailsOut($row,$type); ?> <? if($type == 1) imageOut($row); else detailsOut($row,$type); ?> </tr> </table> <? } ?> <? } ?> </td> Apologies for the code being so long Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/ Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 my head hurts... twas too long to read. do you have any shorter description for that? sorry, maybe i was too lazy to read long codes. Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-563740 Share on other sites More sharing options...
stublackett Posted June 12, 2008 Author Share Posted June 12, 2008 Haha, I did apologise for the long code Basically I need the <?=$_SESSION['total_price']; ?> To collate to the first bit of code which calculates the discount IF the Voucher code "TQ" has been entered which is <?php //Check for discount $amount = $_SESSION['total_price'];; $discount = $_GET['value']; if ($discount == "TQ") { $total = $value; $discount = 0.9; $newtotal = $amount * 0.9; echo $newtotal; } ?> Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-563743 Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 i dont know if i get it right but lemme try. what you want to do is to change the value of the session variable total_price with the new total price given there was a discount, right? well you have that code working but you havent changed your session variable at all. you might want to: <?php //Check for discount // removed unnecessary semicolon: $amount = $_SESSION['total_price'];; <-- unnecessary $amount = $_SESSION['total_price']; $discount = $_GET['value']; if ($discount == "TQ") { $total = $value; $discount = 0.9; $newtotal = $amount * $discount; // changed to variable intstead // save back to session variable total_price $_SESSION['total_price'] = $newtotal; echo $newtotal; } ?> did i get it right? Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-563750 Share on other sites More sharing options...
xtopolis Posted June 12, 2008 Share Posted June 12, 2008 Is it because your $discount == "TQ" is never true? If you supply a code TQ###, discount will never == TQ only Or are you just saying 'for example'? If the codes are TQ + ##....shouldn't you take a substr($_GET['value'],0,2) to see if the code starts with TQ... http://xtopolis.com/z_phpfreaks/session/?value=TQ123 Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-563752 Share on other sites More sharing options...
stublackett Posted June 12, 2008 Author Share Posted June 12, 2008 i dont know if i get it right but lemme try. what you want to do is to change the value of the session variable total_price with the new total price given there was a discount, right? well you have that code working but you havent changed your session variable at all. you might want to: <?php //Check for discount // removed unnecessary semicolon: $amount = $_SESSION['total_price'];; <-- unnecessary $amount = $_SESSION['total_price']; $discount = $_GET['value']; if ($discount == "TQ") { $total = $value; $discount = 0.9; $newtotal = $amount * $discount; // changed to variable intstead // save back to session variable total_price $_SESSION['total_price'] = $newtotal; echo $newtotal; } ?> did i get it right? Kinda...... <h1>Total: £<?=$_SESSION['total_price']; ?></h1> Its that bit thats not changing at all The form is going to a seperate page also and is displaying the echo, Is there anyway to get the form to process itself? You might get a good idea of what I'm after by going to : http://www.toquesnuff.com/shop.php?view=cart and in the Discount Code area type in "TQ" I am just using TQ as an example xtopolis yeah, I will do something similar to the $_GET['value'] you have suggested, Thanks Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-563754 Share on other sites More sharing options...
stublackett Posted June 12, 2008 Author Share Posted June 12, 2008 Just to update I've got this working now....... As xtopolis pointed out, I need something that now takes in TQ01 all the way to TQ9999 Whats the best way to lay this out? My current code is : <?php //Check for discount $amount = $_SESSION['total_price']; $discount = $_POST['value']; if ($discount == "TQ") { $total = $value; $discount = 0.9; $newtotal = $amount * 0.9; $newtotal = round($newtotal, 2); // save back to session variable total_price $_SESSION['total_price'] = $newtotal; } if ($discount == "tq") { $total = $value; $discount = 0.9; $newtotal = $amount * 0.9; $newtotal = round($newtotal, 2); // save back to session variable total_price $_SESSION['total_price'] = $newtotal; } ?> Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-563936 Share on other sites More sharing options...
xtopolis Posted June 12, 2008 Share Posted June 12, 2008 That depends, will the codes expire once they've been used? If one person uses TQ01, can no one else use TQ01 again? Either way, first if the determined format is TQ + #(up to 4#s), you would use a regular expression to match. Check this site out for a decent regular expression explanation. I tend to favor preg_ style btw. http://www.webcheatsheet.com/php/regular_expressions.php <?php $code = $_GET['value'];// YOU WOULD WANT TO FURTHER VALIDATE/TYPE SAFE THIS VALUE $pattern = '/^TQ([0-9]{1,4})/'; //This pattern looks for: "Starts with "TQ" "Followed by 1 to 4 #s" only. It is case sensitive. //To make it insensitive (TQ == tq) add the letter "i" at the end $pattern = '/^TQ([0-9]{1,4})/i'; if(preg_match($pattern,$code)) { //Do something more to check code } ?> If codes are reusable, you could probably just discount the price right there. If they get burnt right after use, you would store those in a database after successful entry. And in the //do something more to check section, you would create a query looking for if that code already exists in the database. If yes, you would error : "code has already been entered, please enter a different one", otherwise you would write it to the database, and apply the discount. Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-564179 Share on other sites More sharing options...
DyslexicDog Posted June 12, 2008 Share Posted June 12, 2008 I didn't read most of the code but I'm guessing this is running out of a database of some sort. If it were me I would create a coupons table that the site owner could add coupons to certain members for x number of uses. This leaves a lot of room to add coupons later, and they could be restricted so that only a specific member has access to them. It's another page that would need to be added to the admin area, but it's more billable work too. Link to comment https://forums.phpfreaks.com/topic/109859-shop-facility/#findComment-564206 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.