TD
< Back to home

Foxycart & MODx – Adding live cart contents to your site

Posted by Sam Clarke on Saturday Jul 10, 2010 Under Development

I’m currently working on a redesign and rebuild of Little Plum, and the CMS of choice has been MODx. The cart we’ve chosen to use is FoxyCart, due to how awesomely customisable it has turned out to be. I’ve noticed quite a lack of tutorials on using modx and foxycart together on the web, so after this experience with the Little Plum store I will try and put a few online.

In this post I’m just going to briefly explain how you can get live figures from your FoxyCart cart on your website using the FoxyCart JSON cart object. I’ll get to work on some more in depth tutorials in the future, so keep checking back.This quick tutorial assumes you’ve got your store set up and the foxycart includes already embedded on your page. Check out the foxycart documentation if any info is needed on how to do this.

When you update your cart, FC will call the fc_BuildFoxyCart() function, so whatever we put within this function will be triggered after any interaction with the cart. Create a new js file and insert this function. Include the new js file on your website.

function fc_BuildFoxyCart() {

}

So what we want to do in this example, is display how many products we have in our cart, and what the total price is. We want to display this in a div on our website, so we’ll create a div we can write the data to.

<div id="minicart"></div>

In this case, our div id is “#minicart”

Getting data from the foxycart JSON is very simple, in this example we want the product_count and the total_price:

So our fc_BuildFoxyCart() function becomes:

function fc_BuildFoxyCart() {
        if (fc_json.product_count > 0) {
            $("#minicart").html("<p>"+fc_json.product_count+" items, &pound;"
            +fc_json.total_price+"</p>");
	}
	else {
            $("#minicart").html("<p>0 Items</p>");
        }

        if (fc_json.product_count == 1) {
            $("#minicart").html("<p>"+fc_json.product_count+" item, &pound;"
            +fc_json.total_price+"</p>");
	}
}

Go ahead and update your cart, and watch the details written to the #minicart div. The if handles no items, and a single item., but you can customise this how you like.

This should have given you some basic knowledge on the FoxyCart JSON and how we can use it to get data from the cart onto our sites without needing to access the cart popup itself. For a full view of the JSONstructure, check out this documentation. I’ll be writing some proper tutorials very soon on FoxyCart and modx, so keep checking back!

TD

2 Responses to “Foxycart & MODx – Adding live cart contents to your site”

  1. Nathan Pitman Says:

    Hi Sam, this is a really helpful write up so thanks for posting it. I’m using this as a starting point to notify users whether an item is already in their cart on a site I’m working on and wondered if you knew how to deal with a situation where you already have ‘fc_BuildFoxyCart’ defined elsewhere.

    In this case I have a 3rd party solution that already provides a FoxyCart minicart and by adding my own JavaScript function using this same function name I break the functionality it provides… I guess simply because the function has been redeclared… I’m wondering if there’s some way in JS to extend an existing function?

    Thanks in advance for any help but understand if this is beyond the call of duty. :)

  2. Sam Clarke Says:

    Hi Nathan,

    Thanks for your comment, sorry I’ve taken a while to respond. I’ve not tried that myself, but you could redefine the original function, create your new custom fc_BuildFoxyCart then call the original redefined fc_BuildFoxyCart afterwards? Which 3rd Party software are you using? Foxee?

    You could try something like…

    var new_fc_BuildFoxyCart = fc_BuildFoxyCart;

    function fc_BuildFoxyCart()
    {
    alert(“new function! hooray!”);
    return new_fc_BuildFoxyCart();
    }

    Hope that helps a bit.

Leave a Reply