Hi FoxyFellas,
I've got a rather complex setup where I have to run some math on the cart contents and output total savings, etc., plus I'd like to have thumbnails in the cart.
Please feel free to tell me if this is dumb, but I'm thinking of building my own cart using an ExpressionEngine teomplate. This would allow me to parse the EE template tags and do the things I need to do. Can I grab the cart contents with JSON on this EE page, or...is that just the worst idea you've seen in a long time?
(Brett: Hey Luke, get a load of this guy -- we built him a cart platform and he's trying to build his own cart...within our cart...lol)
You can drop in an ikea url like this one: http://www.ikea.com/us/en/catalog/products/90133982 and it will do some cool stuff in the background for you.
Check these links for more info:
http://wiki.foxycart.com/docs/json
http://wiki.foxycart.com/docs/json/tutorial
http://wiki.foxycart.com/docs/jsonp
The docs say the JSON object is loaded whenever foxycart_includes.js is called...which is on all of my pages. In order to access this object, do I need to explicitly call it, like this...
...and then use the jQuery html() function to dynamically write the cart contents to my EE template...or is there a better way?
We had a store that was doing things incorrectly calling the json cart 6 times with every page load! When they got blown up on gizmodo, tauw, cnet, etc... no one was happy because we were getting hammered. So yeah, please limit the calls to the json cart to only what you absolutely need.
My approach is to use ExpressionEngine's (EE's) tags to build the cart instead. This is the only way I can have all the flexibility I need. Process would be like this:
1. Store the EE product IDs as a custom option
2. Grab the EE product IDs from the JSON object and parse with PHP
3. Embed the EE product IDs in the EE tags (so we can pull the correct products)
I have to use PHP because I can parse PHP before the EE tags.
In an effort to spare you guys some time on this next question, I did a bunch of homework trying to find an answer, but had no luck. I'm trying to read the FoxyCart JSON object into a PHP file so I can parse it. I know the object is sitting there; I'm just not sure how to grab it.
I tried this:
But that outputs a 0, even though I verified there is a total weight by visiting that URL.
Doing the following:
Outputs:
So it looks like only session_id is populated. Maybe file_get_contents isn't the way to go...?
I'm sorry to pester you guys with this; I know it's more of a PHP question. I ask here because you know the FC JSON object better than anyone, and I want to make sure I'm calling it only when I need to (see previous post by Luke).
Thanks - what you're saying kinda makes sense. What I don't understand is that the session ID is the only thing that actually shows up when I use file_get_contents to load the JSON. If the JSON is already present on the page I'm using (the page that has the PHP snippet in it that loads the JSON) and it has the session ID, why would I need to pass the session ID to the JSON to load it? Can't I just...reach out and grab what's already there?
I guess the more important determination is getting the FCSID and appending it to that URL. I read the docs on JSONP and it says something about adding the session ID using fc_AddSession(). Is that what you're talking about?
I'll try working that function into my PHP snippet later on tonight, so if I get to that before I hear back I'll report my findings. Thanks again for your help.
Please disregard my previous message. I followed the tutorial here and got a custom cart working at my own URL (http://www.example.com/cart). Adding products via JSON works like a charm and everything loads great there. There are just two catches:
1. When clicking "add to cart" the form action has to point to http://example.foxycart.com/cart or else no product is added. Maybe this is the correct behavior. Only problem is...
2. When I cache my custom cart template URL (http://www.example.com/cart) it does not work when I load http://example.foxycart.com/cart. When I say "does not work" I mean it does not appear to load the JSON object. The cart skeleton is there, but no products appear.
I know this is pretty vague, so I can whisper actual URLs if helpful. Any help the community can provide would be greatly appreciated.
If I get this working I'd like to write a blog post on building a custom cart and incorporating with ExpressionEngine (partially for my own future reference
The cart wasn't generating at http://example.foxycart.com/cart because jQuery wasn't being called properly. I've got the cart showing up now and working at http://example.foxycart.com, but now the remove links don't work. I've tried recreating the functionality of the default FoxyCart cart; e.g., by making sure the remove links all look like this:
Where X is the item's # in the cart.
I've then made sure that the quantity input field is generated. The input value is set to 0 as fc_RemoveItem() says:
But then the page reloads and the removed item is still in the cart, with the quantity value set as before.
As a side note, we generally won't be around on Sundays USA time so we're not disregarding you on purpose.
Thanks for getting back. I know that you guys aren't responsive on the weekend, so no problem. You need some time to yourselves
You can ignore everything but my most previous message -- I ended up posting and then subsequently solving my own problems.
I managed to get my own custom cart working by following the cart tutorial on this wiki page. The only issue I'm currently having with my custom cart is that the "Remove" functionality does not work. You can see my custom cart here; visit the store here if you need to add some products. I feel better about this cart because I don't need to use PHP or cURL to get to it; simply accessing it via fc_json.[x] works great.
Interestingly, I couldn't get fc_BuildFoxyCart() to fire, so I didn't use it.
[li]
Bath tub #1[/li][li]Bath tub #2[/li]
I made sure to define fc_BuildFoxyCart after including foxycart_includes.js; placed an alert function in there, but that never fires.
1. Using jQuery, build a qty input field that has the same name and ID as the ones in your default cart, like:
<input type="text" class="fc_cart_item_quantity" id="quantity1" name="quantity1" value="1">
2. Build the "Remove" links like:
<a href="#" onclick="fc_RemoveItem(1); return false;" title="Remove this item">Remove</a>
The "Remove" click does set qty to 0, but that might not mean anything since my input fields may not actually be talking to the FoxyCart scripts -- if that makes sense.
I realize I'm out in Custom Land and that I signed myself up for this, so if you don't know where to point me I totally understand. That being said -- any thoughts on how to get my remove links talking with your scripts and the JSON object is appreciated. I'll keep working on it.
As an aside, this cart is by far the most customized FoxyCart implementation I've done, and I continue to be very impressed and amazed at just how flexible and powerful your product is. Thanks again for making FoxyCart a developer's dream!
Setting the quantity is just the first part, then you'll need to actually submit the cart with an update action. Your best bet may be to take a look at the default FoxyCart implementation and use FireBug's net tab to look at what data is being sent back and forth. The links I put in the first reply should have some examples of how to do this via the JSONP too. Hope that helps point you in the right direction.
Just wanted to chime in and say "wow". I loaded up your cart and was thoroughly confused for a moment, because the cart was most definitely at *.foxycart.com, but it wasn't the standard cart. I don't believe anybody's ever taken that approach.
Please vote for this though:
http://requests.foxycart.com/forums/4162-general-requests/suggestions/121058-template-language-editable-cart-checkout-templat?ref=title
Your customized cart is probably a PRIME example of how awesome a full template language would be. (It's one of my pet requests, fwiw, hence the excitement.) Great work so far.
I found the info on removing items from the cart here. The docs say to "send" something like:
If I type in a URL like...
http://example.foxycart.com/cart.php?&cart=update&1:quantity=0&1:id=<product id from json>&2:quantity=0&2:id=<product id from json>
...the product is removed fine. My questions are...do I need to point my form action to a URL like that? Wouldn't that require me writing a function separate from fc_RemoveItem, since that function doesn't work for me? Where is that URL being generated on the default cart? I don't see any submit buttons or hidden fields or form actions pointing to anything like that. I ask because I need to recreate that string in my custom cart for the remove button to work. The thing is, I'm already taking advantage of some of your functions (fc_RemoveItem, fc_PreventCheckout) and don't want to rewrite them if I don't have to.
I hope my question(s) makes sense.
I thought about using the default cart at the beginning, but if you take a look at what I've got (attachment) you'll see a few extra columns and rows that aren't in the default cart. Rather than manipulate everything to death with jQuery and CSS, I figured I'd build from scratch.
I suppose I could use some jQuery functions (like insertAfter and insertBefore) to get my stuff where I want it, but I think it's easier if I figure out a way to get the remove functionality working. I won't bug you guys on this anymore; thanks for all of your help on this. I'm totally down for Brett's idea of a templating language!
@vossavant, I'm kind of with Luke in the sense that I probably would have gone with hacking the existing cart to death rather than rebuilding it, but fwiw I do definitely see why you took the approach you did, and realistically it's probably a better option (and basically is a case study for how awesome a template language would be). Just want to make it clear we're not passing judgement or anything.
Edit: Just noticed that fc_addSession is not defined. Not sure why that is...
I suspect that if I knew what to put where you say "you'd need to remove the relevant row from the table here" then it would work. Would you mind clarifying that piece? I'm almost there, and really appreciate the help you all have lent so far.
@brett,
lol
I understand what you're saying. Reinventing the wheel isn't something I like to do; however, everything outside of the "remove" functionality was pretty easy to get going. A templating language would be more than welcome, but what you're able to do without it is pretty amazing.
The fc_AddSession() is available via the foxycart_includes.js, which isn't loaded on the cart (AND SHOULDN'T BE! it'll actually break things). So replace that with... I think that'd do what you needed for that section.
As to not removing more than one product, I'm not sure about that, but hopefully the two notes above help resolve that issue.
Thanks for your response. I was about to write you another note on how I couldn't get this to work, but I figured it out. Maybe you can understand why this works:
Say I'm trying to remove the 4th item in my cart; I'd pass this:
That wasn't doing anything, so I eventually discovered that having the same item # for every item in the cart works. Thus, no matter which item I want to remove, I pass in #1 for the item:
I hope that makes sense.
I don't need to use the click() function at all. I can verify that the JSON object is empty and the appropriate items are removed after each click. Do you see anything funny with how this works?
1) It used to be based on the numbers like that, but that was awkward so we made it based on IDs.
2) The 1: isn't actually necessary. I've updated the docs, but the 1: and 2: and etc are used if you're modifying multiple quantities at once. If you don't start at 1: though you'll run into problems.
So you're code is fine. You could remove the 1: prefixes if you wanted. That's why it works