Update & checkout buttons for cart

Hi, I have set up a simple shopping cart with a coupon code function.

I am having an issue with the Update basket and Checkout buttons, when you click update basket it loads the /checkout/ page but then redirects back to the /cart/ after a couple of seconds when it should just load a new page saying ‘Shopping basket successfully updated’ - also, when you click the proceed to checkout button it does the same, loads the /checkout/ page (which is correct) but then redirects back to /cart/.

Here is the php code I have setup:

<?php

// View Shopping Basket

if (!isset($_GET['action']) || $_GET['action'] == 'products') {
	// Check if the shopping basket is empty

	if ($cart->get_num_products(session_id()) <= 0) {
		output('Your shopping basket contains no products!');
	} else {
		if (!isset($_POST['submit'])) {
			$tpl->set('body', $body->fetch('index.tpl.php'));
		} else {
			$cart->update_contents(session_id());
			output('Shopping basket successfully updated!');
			redirect('/cart/', 2);
		}
	}
}

// Add Product to Cart

if (isset($_GET['action']) && $_GET['action'] == 'add_product') {
	$product_id = isset($_GET['product_id']) && is_numeric($_GET['product_id']) ? (int) $_GET['product_id'] : 0;
	$cart->add_product(session_id(), $product_id, 1);

	output('Product successfully added! Please wait while we redirect you to your shopping basket.');
	redirect('/cart/', 2);
}

	if (isset($_SESSION['coupon_code'])){
			unset($_SESSION['coupon_code']);
	}
	
		if (isset($_POST['go']) && $_POST['go'] != ''){
			$_SESSION['coupon_code'] = $_POST['coupon_code']; 
			header("location:/checkout/");
		}

?>

Here is the HTML for the form:

<h1>My Basket</h1>
<br />
<form method="post" action="/cart/">
	<input type="hidden" name="submit" value="Update" />
    <input type="hidden" name="go" value="Checkout" />
		<table class="tableGrid">
			<tr>
				<th width="10%"><strong>Remove:</strong></th>
				<th width="60%"><strong>Product Name:</strong></th>
				<th width="10%"><strong>Price:</strong></th>
				<th width="10%"><strong>Qty:</strong></th>
				<th width="10%"><strong>Total:</strong></th>
			</tr>
			<?php foreach ($GLOBALS['cart']->get_contents(session_id()) as $product) { ?>
				<tr>
					<td align="center" width="10%"><input type="checkbox" name="delete<?php echo $product['cart_id']; ?>" value="yes" /></td>
					<td width="60%"><a href="<?php echo get_url('/products/', $product['category_id'], $product['product_id'], $product['product_name']); ?>"><?php echo $product['product_name']; ?></a> <span class="important">* <?php echo $product['stock'] >= $product['quantity'] ? 'Available' : 'Not Enough In Stock'; ?></span></td>
					<td width="10%" align="center"><?php echo get_price($product['price']); ?></td>
					<td width="10%" align="center"><input type="text" name="quantity<?php echo $product['cart_id']; ?>" value="<?php echo $product['quantity']; ?>" maxlength="10" style="width: 30px;" /></td>
					<td width="10%" align="center"><?php echo get_price($product['price'] * $product['quantity']); ?></td>
				</tr>
			<?php } ?>
		</table><br />
        <div class="main_box" style="height: 26px;">
        <div style="float: right;">Subtotal: <strong><?php echo get_price($GLOBALS['cart']->get_cart_total(session_id())); ?></strong></div>
		<div style="float: left;">
			<label><strong>If you have a promotion voucher code please enter it here: </strong></label>
			<input type="text" name="coupon_code" id="coupon_code" value="" />
		</div>
        </div><br />
		<div align="center"><input type="image" src="<?php echo '/' . $config['image_path']; ?>button_cart_update.gif" border="0" alt="Update Shopping Basket" /> <a href="/shop-11/"><img src="<?php echo '/' . $config['image_path']; ?>button_continue.gif" border="0" alt="Continue Shopping" /></a> <input type="image" src="<?php echo '/' . $config['image_path']; ?>button_checkout.gif" border="0" alt="Proceed to Checkout" /></div>
</form>

If anybody can spot whats wrong I would really appreciate it. - It is probably something so simple but I am finding ti quite frustrating.

Thanks,

Paul

If you do a JavaScript redirection, I would also do an event listener onKeyUp and reset the timer if someone were typing. This should give them some time to hit the submit button after typing the last letter.

I have the javascript setup for the feedback part :slight_smile:

Tested the input buttons - worked fine :slight_smile: … Can you only style an input submit button rather than use an image?

You will need to make the Update and Checkout separate inputs that are of type submit rather than hidden. By doing so each will exist in the $_POST array if it was clicked. That will resolve the issue with both always being present.

The redirect issue is due to the the redirect happening immediately. You would need to use JavaScript to set a timeout for the redirect occur after a certain period of time to display some feedback on the same page before moving to the next.

Does anyone maybe know a tidier, or more senisble way of splitting the php cope into 3 sections for the update cart, proceed to checkout and add products?

Cheers :slight_smile:

Ok - I changed the the value to =“true” … still not much joy.

I am unsure on what you mean with regards to the products & add_products GET action - can both be true at the same time?

<input type="hidden" name="go" value="Checkout" />

Coupled with

if (isset($_POST['go']) && $_POST['go'] != ''){

}

Will always be true, so it will always go to /checkout

When it comes to redirecting, you usually need to exit/die so that the script does not continue.

Most likely your script has an incorrect logic path, since you have a bunch of If that don’t seem to be setup properly.

For instance:

if (!isset($_GET['action']) || $_GET['action'] == 'products') {

and

if (isset($_GET['action']) && $_GET['action'] == 'add_product') {

can both be true at the same time.

Maybe someone with more time can help you better but that’s what I see from glancing at your code.