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 &quot;Quantity&quot; 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?

    10 days later

    Hi - Interesting problem.

    Have you tried using $POST instead of $REQUEST when retrieving the promocode?

    I would expect that, provided the originating text box was within the same form as the rest that that should resolve the issue.

    Also, do try a var_dump of $_POST and other globals to see whether the data is being sent at all. This will detect HTML problems. I have often misplaced a </form> tag, especially when adding to existing code. If the data is not being sent, view the source of your web page in the browser and check that it is actually as it should be. Try it in Firefox as well as IE. The former tends to be somewhat more understanding - if it works in one but not the other, It's the HTML at fault.

    There may be other issues in the PHP but it would be good to know that the data was sent before picking through it.

    Rob

      Hi,

      Thank you for taking the time to look over this problem.

      I actually managed to solve this about two days ago. I came to realise that in order to pass the information through then perhaps it should be stored temporarily. What I forgot was that my shopping cart script made a row in the SQL database related to the session_id of the user. This row held what item id it held, session id and qty required. So when I was updating the quantity, what I actually was doing was updating the row in the table.

      To solve the discount problem, I just added a new column to the table so that if a user entered a discount code it would hold it there until the order is completed. That way the user can only enter one discount code for the entire order, which in a way is handy.

      Thanks to all who took time to read this.

        Write a Reply...