Simple Cart "Continue" Button

8 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: April 3, 2014   (RSS)

By Perchpole - March 28, 2014

Hello, All

I've been struggling to work out how to create a "Continue" button for SimpleCart. When you add a product you are automatically forwarded to the cart pagge. I want to give people the option of going back to the previous page where they came from. (In so doing, I also wanted to ensure that the inventory of the product would be updated.)

After lots of trial and error I came up with this code:

<?php $url = htmlspecialchars($_SERVER['HTTP_REFERER']); ?>
<input type="submit" name="update" value="Continue" onClick="window.location='<?php echo $url ?>'; return false;" />

The button takes the customer back to the previous product and reloads the page. It worked perfectly - until I discovered a major problem...

If whilst on the cart page the customer decides to change the totals and hits the "Update" button, the page history is also updated. The previous page is now the cart page itself and so the "Continue" button doesn't work!

I've tried loads of ways to get around this but nothing seems to work. I've even tried storing the product page as a $_SESSION variable but these appear to be wiped when the cart page loads.

Any help would be most welcome!

:0/

Perch

By Chris - March 31, 2014 - edited: March 31, 2014

Hi Perch,

Since it's possible for users to disable HTTP_REFERER in their browsers, I always prefer to explicitly set return URLs with query strings or form data. You can use thisPageUrl() to get a link to the current page. In a link (for example, the ones in examples/sc-cart-add.php), you can do this:

<a href="<?php echo $addLink ?>&returnUrl=<?php echo htmlencode(urlencode(thisPageUrl())) ?>">Add to cart</a>

...or, in a form (also from the same page):

<input type="hidden" name="returnUrl" value="<?php echo htmlencode(thisPageUrl()) ?>">

Then, you'll need to process that and store it. In general, cookies are more reliable than sessions (due to various server configurations), so I'd suggest using them. Note that both the links and the forms on sc-cart-add.php both submit to sc-cart-add.php, which then redirects to sc-cart.php: be careful that you're not trying to capture the returnUrl after the browser has already been redirected and the query string has been lost. We'll need to capture the returnUrl before that redirect happens, so add this to the (very) top the target page (e.g. sc-cart-add.php):

// record returnUrl, if provided
if (@$_REQUEST['returnUrl']) {
  setcookie('returnUrl', $_REQUEST['returnUrl'], strtotime('+10 years'), '/');
}

Now you can use the cookie's value as the target URL on your cart pages (e.g. sc-cart.php). Note that I'm defaulting the URL to / with coalesce() below:

<?php $returnUrl = coalesce(@$_COOKIE['returnUrl'], '/') ?>
<a href="<?php echo htmlencode($returnUrl) ?>">Back to <?php echo htmlencode($returnUrl) ?></a>

You can test this by adding a junk query string to your page with the add link/form (e.g. sc-cart-add.php?hello), then making sure that the resulting link goes back to your unique url (e.g. ?hello).

Hope this helps! Please let me know if you have any questions.

All the best,
Chris

By Perchpole - March 31, 2014

Chris -

Thanks for your help. I'm having some success with your method - but something is a bit quirky!

I can only set the cookie if one hasn't been set already. Once set it cannot be over-written. So it works the first time - but then just comes up with the same URL thereafter until I clear the cookie manually.

:0/

Perch

By Chris - April 1, 2014

Hi Perch,

That's peculiar. Do you have a URL I can check out to see the problem?

All the best,
Chris

By Perchpole - April 1, 2014

Hi, Chris -

Try to buy this product:

http://www.zimouk.co.uk/index.php?prod=6

Perch

By Perchpole - April 3, 2014

Hi, Chris -

I had a trawl around the Tinternet and found some useful info. The issue seems to boil down to is the if() statement around the code.

if (@$_REQUEST['returnUrl']) {
  setcookie('returnUrl', $_REQUEST['returnUrl'], strtotime('+10 years'), '/');
}

The $_REQUEST array contains all request variables, including both GET and COOKIE values. So, if the COOKIE has been set then the value of returnUrl is just replacing itself - or something like that!

Anyhow, I have added the following to the top of my product pages and it seems to work:

$return = htmlencode(thisPageUrl());
setcookie('returnUrl', $return, strtotime('+10 years'), '/')

:0)

Perch

By Chris - April 3, 2014

Hah! That's a clever solution!

Glad you got his working! :)

All the best,
Chris