startSectionEdit: $data "plugin_data" is NOT an array! One of your plugins needs an update.
type:
integration
supports-foxycart-version-from:
0.7.2
system:
FoxyCart
name:
Saved Cart
description:
Save the cart contents as a persistent cart between sessions.
tag:
cart, saved, persistent
date:
2012-12-06
version:
1.1
developer:
http://www.foxy-shop.com/

FoxyCart Persistent Cart Script

Please note: The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Please note: This code works with FoxyCart 1.0 and 1.1. It is not compatible with FoxyCart 2.0+.

Description

This script will save a copy of your cart's JSON in a local cookie for a specified amount of time. Any time your cart is changed, this local cookie will be updated. If the local cookie exists but there are 0 products in your cart, the script will assume this is a return user and attempt to populate your new fcsid session with the contents of the local cart cookie.

Validation

If your FoxyCart store uses HMAC product validation, you'll need use the attached PHP file in addition to the JS file. The PHP file encrypts the replacement cart and then loads the products and coupons one by one. Load your store domain and API key into the PHP file. Be sure to set the path to this file in your savedcart.js file.

If you are not using validation you do not need to load the savedcart.php file to your site.

Installation

Fill out the settings section of savedcart.js with your preferences. The default settings don't need to be adjusted if you aren't using HMAC validation. Set the src value and place this code in the footer of all pages on your site:

<script type="text/javascript" src="path/to/savedcart/savedcart.js"></script>

The native limitation of this script is that it doesn't know the difference between an FC session that has been completed and one that has simply expired. So if the customer completes a checkout and the session is removed, the cart will automatically be repopulated once they get back to the site. To get around this, it is recommended that you set a “?ordercomplete=1” at the end of your thank-you url. The script is designed to remove the local saved cart cookie if it finds “ordercomplete=1” in the browser querystring. This isn't foolproof because a user may just click a link back to the site–or not go back to the site at all in the same session.

Testing

To test, simply add some items to your cart, close the browser and restart. If everything is working, your cart should be repopulated.

Requirements

  • PHP Version 5.1.2 (only required if using validation)
  • Tested with jQuery 1.8.2 – should be fine with older versions
  • Tested with FoxyCart 1.0 – should be fine with 0.7+
  • NOT compatible with FoxyCart 2.0

Other Notes

This script requires jQuery. If jQuery isn't being loaded by your site, you can place this in your header:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

This script also uses the following scripts:

  • jQuery Cookie plugin (to read and set cookies)
  • json2.js (to convert a JSON array to text using JSON.stringify on IE6-7 that don't support this natively)

If either of these plugins are already being loaded by your site you can simply remove the version found in savedcart.js to avoid conflicts. You can also set debugging mode to “true” to see exactly what the script is doing using console.log.

If using WordPress, this can be placed in your functions.php file for automatic inclusion in the footer:

//Saved Cart
add_action('wp_enqueue_scripts', 'foxycart_saved_cart_script');
function foxycart_saved_cart_script() {
	wp_enqueue_script('foxycart_saved_cart_script', get_bloginfo("stylesheet_directory") . '/savedcart.js', null, '1.0', true);
}

Have a question or found a bug? Feel free to write to David by using the contact form at http://www.foxy-shop.com/.

Happy trails!

Download

Site Tools