Hi All,
As I posted a while ago about a cart script I had, my client has decided that he would like a discount code applied to the checkout. So when he's running offers he's going to hand out a specific code to people to get say 10% off the order.
I've worked out the percentage parts not a problem and tested it to make sure it works fine. However, it's my move to the next step thats confusing me.
I've got a table set up in the database for the voucher names, discount percentage, start date and end date. I've made the sure connection works fine and the expiration stuff all works fine.
So the problem I'm now having is putting the text field into the checkout to allow the user to enter in a discount code. I've got the field on the page, but if I type a correct code into the field and hit 'Update Cart' no discount is applied. Here's the important parts to the script.
cart.php
if(isset($enter_code)){
$code_grab = mysql_query("SELECT * FROM tbl_voucher WHERE name='$enter_code'")
or die(mysql_error());
while($row_1 = mysql_fetch_array($code_grab)){
$code_name = $row_1['name'];
$discount = $row_1['discount'];
$start_date = $row_1['start'];
$end_date = $row_1['end'];
$today_date = date("Y-m-d");
if($end_date > $today_date){
$valid = 'yes';
$apply_dis = $discount;
} else {
$valid = 'no';
$apply_dis= 0;
}
}
}
cart.php actually calls another file full of functions, and when update is pressed, it runs an update function as per:
function updateCart()
{
$cartId = $_POST['hidCartId'];
$productId = $_POST['hidProductId'];
$itemQty = $_POST['txtQty'];
$enter_code = $_REQUEST['promocode'];
$numItem = count($itemQty);
$numDeleted = 0;
$notice = '';
for ($i = 0; $i < $numItem; $i++) {
$newQty = (int)$itemQty[$i];
if ($newQty < 1) {
// remove this item from shopping cart
deleteFromCart($cartId[$i]);
$numDeleted += 1;
} else {
// check current stock
$sql = "SELECT pd_name, pd_qty
FROM tbl_product
WHERE pd_id = {$productId[$i]}";
$result = dbQuery($sql);
$row = dbFetchAssoc($result);
if ($newQty > $row['pd_qty']) {
// we only have this much in stock
$newQty = $row['pd_qty'];
// if the customer put more than
// we have in stock, give a notice
if ($row['pd_qty'] > 0) {
setError('The quantity you have requested is more than we currently have in stock. The number
available is indicated in the "Quantity" box. ');
} else {
// the product is no longer in stock
setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock');
// remove this item from shopping cart
deleteFromCart($cartId[$i]);
$numDeleted += 1;
}
}
// update product quantity
$sql = "UPDATE tbl_cart
SET ct_qty = $newQty
WHERE ct_id = {$cartId[$i]}";
dbQuery($sql);
}
}
if ($numDeleted == $numItem) {
// if all item deleted return to the last page that
// the customer visited before going to shopping cart
header("Location: $returnUrl" . $_SESSION['shop_return_url']);
} else {
header('Location: cart.php');
}
exit;
}
and this is what I'm using in cart.php to display the text field:
<input name="promocode" id="promocode" type="text" class="box">
Hopefully I've put enough there to work on. The cart script has a field to update quantity and I've followed the same process but for some reason I can't get it to pass the code on update.
Any guidence?