Jump to content
Larry Ullman's Book Forums

Adding Voucher Code To Basket


Recommended Posts

I have tried (without much success) to add a voucher code to the basket as in the book, but it is not updating the discount. Does anyone have any advice on what I have done wrong. I am using phpmmyadmin and have created the fields in here.

 

<?php
session_start();
$_SESSION['cart'];
?>
    <?php #script together. This combines addtocart and view cart.
if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) {
$pid = (int) $_GET['pid'];
if (isset($_SESSION['cart'][$pid])) {
 $_SESSION['cart'][$pid]['quantity']++;
}else{ //new product
require_once ('../mysqli_connect.php');
$q="SELECT price, product_name FROM product WHERE product_id=$pid";
$r=mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) {
list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM);
$_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price, 'product_name' => $product_name);
}else{
echo '<div align="center">Your basket has been updated</div>';
}
}
}else{ //no print ID
echo '<div align="center"> Your basket has been updated</div>';
}
if (isset($_POST['submitted'])) {
foreach ($_POST['qty'] as $k => $v) {

 $pid = (int) $k;
 $qty = (int) $v;

 if ($qty ==0) { // Delete.
  unset ($_SESSION['cart'][$pid]);
 } elseif ($qty >0 ) { // Change quantity.
  $_SESSION['cart'][$pid]['quantity'] = $qty;
 }
  }
}
if (!empty($_SESSION['cart'])) {
require_once ('../mysqli_connect.php');
$q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN (";				 foreach ($_SESSION['cart'] as $pid => $value) {
 $q .= $pid . ',';
}
$q=substr($q, 0, -1) . ') ORDER BY product_name ASC';
$r = mysqli_query ($dbc, $q);
 // Create a form and a table:
echo '<form action="basketv.php" method="post">
<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
 <td align="left" width="30%"><b>Description</b></td>
 <td align="left" width="30%"><b>Product Name</b></td>
 <td align="right" width="10%"><b>Price</b></td>
 <td align="center" width="10%"><b>Qty</b></td>
 <td align="right" width="10%"><b>Total Price</b></td>
</tr>
';
$total =0; // Total cost of the order.
$postage=1.00;
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
 $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price'];
 $total1 += ($subtotal);
 // Print the row.
 echo "\t<tr>
 <td align=\"left\">{$row['description']}</td>
 <td align=\"left\">{$row['product_name']}</td>
 <td align=\"right\">£{$_SESSION['cart'][$row['product_id']]['price']}</td>
 <td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td>
 <td align=\"right\">£" . number_format ($subtotal, 2) . "</td>
</tr>\n";

}	 $total += ($postage + $total1);
mysqli_close($dbc);
echo '<tr>
   <td colspan="4" align="right"><b> Postage:</b></td><br/>
 <td align="right">£' . number_format ($postage, 2) . '</td>
 <br/>
 <br/>
 <br/>
 <br/>
 <td colspan="4" align="right"><b>Voucher Discount:</b></td>
 <td align="right">£' . number_format ($voucher, 2) . '</td>
 <td colspan="4" align="right"><b>Total:</b></td>
 <td align="right">£' . number_format ($total, 2) . '</td>
</tr>
</table><p align="left">Enter a quantity of 0 to remove an item.
<div align="left"><input type="submit" name="submit" value="Update" /></div>
<input type="hidden" name="submitted" value="TRUE" />
<p align="center"><a href="logincustomer.php"><img src="checkout.gif" width="83" height="27" border="0"/></a></p>';
} else {
echo '<p>Your cart is currently empty.</p>';
}
?>
 <?php
{
if( $voucherCode != '' ){
// we need the date, to see if the voucher has expired
$cts = date('Y-m-d H:i:s');
$q = "SELECT *, if('{$cts}' > expiry, 1, 0)AS expired FROM discount_codes WHERE vouchercode='{$voucherCode}' LIMIT 1";
 $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 if (@mysqli_num_rows($r) == 0)
 { // A match was made.
 $row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
echo '<p class="error">Sorry, the voucher code you entered is invalid.</p>';
}else{
if( $voucher['active'] == 1 )
{
if( $voucher['expired'] == 1 )
{
 echo '<p class="error">Sorry, this voucher has expired</p>';
return false;
}
else
{
if( $voucher['num_vouchers'] != 0 )
{
if( $subtotal >= $voucher['min_basket_cost'] )
{
$discountCode = $voucherCode;
$discountCodeId = $voucher['ID'];
if( $voucher['discount_operation'] == '%' )
{
$subtotal =
$subtotal - (($subtotal)/100)*$voucher['discount_amount'];
$voucher_notice = 'A '
. $voucher['discount_amount']
. '% discount has been applied to your order';
return true;
}
elseif( $voucher['%'] == '-' )
{
$subtotal =
$subtotal - $voucher['discount_amount'];
$voucher_notice = 'A discount of £'
. $voucher['discount_amount']
. ' has been applied to your order';
return true;
 }
 }
 else
 {
  echo '<p class="error">= Sorry, your order total is not enough for your order to qualify for this discount code</p>';
  return false;
  }
  }
  else
  {
   echo '<p class="error">Sorry, this was a limited edition voucher code, there are no more instances of that code left</p>';return false;
   }
   }
   }
   else
   {
 echo '<p class="error">Sorry, the voucher code you entered is no longer active</p>';return false;}}}}
?>
			 <form action="basketv.php" method="post" >
<p><b>Voucher code:</b> <input type="text" name="voucher" size="20" maxlength="40" /></p>
	    <input type="submit" name="submit" value="Submit" />
	    </form>

Link to comment
Share on other sites

That's a lot of code for other people to just look through blindly. How about you start by providing the requisite details, such as the versions in use. Then maybe describe the changes you made to the database and how you think it's supposed to work. Then indicate what isn't working/what is, etc.

 

By providing us with more details, we can provide an educated answer without wasting too much time, rather than grasp at straws.

Link to comment
Share on other sites

phpMyAdmin - 2.11.9.5.

 

I am sure that the database is correct. The first bit of code is from the book (cart) and the second bit of code is for the voucher code, which I have tried to use from the cart code. When the voucher code is entered into the box, it displays

Sorry, this was a limited edition voucher code, there are no more instances of that code left

Below is the code that I tried to create for the voucher code. Thanks for advice on where I have gone wrong.

 [/size]

[size=3]<?php
////// VOUCHER STARTS HERE ///////////////////////
if (isset($_POST['submitted'])){
//require_once (MYSQL);
//if( $voucherCode != '' ){
// we need the date, to see if the voucher has expired
$cts = date('Y-m-d H:i:s');
$q = "SELECT *, if('{$cts}' > expiry, 1, 0)AS expired FROM discount_codes WHERE vouchercode='{$voucherCode}' LIMIT 1";[/size]
[size=3]  $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 if (@mysqli_num_rows($r) == 0)
 { // A match was made.
 //$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

//echo '<p class="error">Sorry, the voucher code you entered is invalid.</p>';
//}else{
//if( $voucher['active'] == 1 )
//{
if( $voucher['expired'] == 1 )
{
 echo '<p class="error">Sorry, this voucher has expired</p>';
return false;
}
else
{
if( $voucher['num_vouchers'] != 0 )
{
if( $subtotal >= $voucher['min_basket_cost'] )[/size]
[size=3]{
$discountCode = $voucherCode;
$discountCodeId = $voucher['ID'];
if( $voucher['discount_operation'] == '%' )
{
$subtotal =
$subtotal - (($subtotal)/100)*$voucher['discount_amount'];
$voucher_notice = 'A '
. $voucher['discount_amount']
. '% discount has been applied to your order';
return true;
}
elseif( $voucher['%'] == '-' )
{
$subtotal =
$subtotal - $voucher['discount_amount'];
$voucher_notice = 'A discount of £'
. $voucher['discount_amount']
. ' has been applied to your order';
return true;
 }
 }
 else
 {
  echo '<p class="error">= Sorry, your order total is not enough for your order to qualify for this discount code</p>';
  return false;
  }
  }
  else
  {
echo '<p class="error">Sorry, this was a limited edition voucher code, there are no more instances of that code left</p>';return false;
}
}
}
else
{
 echo '<p class="error">Sorry, the voucher code you entered is no longer active</p>';return false;}}//}//}
?>
			 <form action="basketv.php" method="post" >
<p><b>Voucher code:</b> <input type="text" name="voucher" size="20" maxlength="40" /></p>
	    <input type="submit" name="submit" value="Submit" />
	     <input type="hidden" name="submitted" value="TRUE" />
	    </form>[/size]
[size=3] [/size]
[size=3]
Link to comment
Share on other sites

Okay, the version of phpMyAdmin isn't relevant. What is relevant are the versions of PHP and MySQL. It's also relevant what changes you made to the database, but only if you actually want help. Also, you really, really need to be putting comments in your code so that others can understand it, and more importantly, so that you can understand it when you look at it later. And you shouldn't be creating the date in PHP and then using it in a query: just create the correct date in MySQL in your query directly.

 

With the lack of comments, the inconsistent formatting, and the parts of the code that are commented out, it's impossible to know what's what. What is the IF condition that ends up with the "this was a limited edition voucher" being printed?

Link to comment
Share on other sites

Hi Larry

 

Thanks for the reply. Do you have any plans to create the code for adding vouchers for people and for example free if ordering over £30? I think that this would be a good addition or extra for the book.

 

I created a new table (discount_codes):

num_vouchers
int(11)
expiry
timestamp
discount_amount
float
min_basket_cost
float
active
tinyint(1)
vouchercode
varchar(25)

ID

int(11)

 

MySQL client version: 5.0.77

Link to comment
Share on other sites

 Share

×
×
  • Create New...