Jump to content

update & checkout buttons for cart


jpopuk

Recommended Posts

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:

 

// 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>
<form method="post" action="/cart/">
<input type="hidden" name="submit" value="Update" />
    <input type="hidden" name="go" value="true" />
	<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" name="checkout" 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

Link to comment
https://forums.phpfreaks.com/topic/214951-update-checkout-buttons-for-cart/
Share on other sites

This:

 

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

Will always execute, because of

 

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

 

If I had to guess, your "redirect" function (which you haven't posted), is setting header("Location: ...") but not terminating the script execution. If you set a header redirect more than once, the last set will take effect.

 

Quick fix? Change

 

redirect('/cart/', 2);

to

 

redirect('/cart/', 2);
exit;

I tried the exit; suggestion but then it loads a white blank page for a couple seconds and then returns to the cart (it did the up both the update, and proceed to checkout buttons).

 

Here is the function for the redirect if this helps:

 

function redirect($path, $timeout = 2) {
header('Refresh: ' . $timeout . '; URL=' . str_replace('&', '&', $path));
}

 

I didn't realise something like this would be quite so difficult :) - the joys of php!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.