Most of this was written by steeeeephen, the developer of Narwhal Company’s website. We have a huge crush on him. Read his description of the site’s requirements and his solutions, which might give you some ideas of the power of FoxyCart’s JSON and XML.

Narwhal Co sells mostly one-of-a-kind hand-made accessories made from recycled vintage ties. Because of this, a flexible purchasing system had to be implemented. The system also had to be very transparent, as the look and feel of the site were a high priority. All the shopping cart systems I had tried were either overly cumbersome to setup and work with, or created nightmarish HTML that required hours of tedious trimming and debugging to produce decent looking pages (and code for that matter). Even the stores that bragged about being standards-compliant generate some pretty scary HTML. After weeks of searching for an e-commerce solution that would fit our requirements, we stumbled across FoxyCart. I took the 2-minute tour and that was all it took to realize I had finally found the right system.

foxycart as an “ecommerce framework”

JSONAJAX, AND PRODUCT LOCKING

Because most of the products in the Narwhal Co. store are one-of-a-kind, it was important to ensure that two people didn’t purchase the same one item. Through use of some AJAX requests, items that are added to a user’s cart they are locked in the database so nobody else can purchase the item for a certain time period: 15 minutes from the user’s last activity on the site.

XML AND INVENTORY

The inventory system works off of the built-in FoxyCart datafeed. Every time the datafeed is sent, a script goes through the datafeed and makes a list of all the items purchased in the transaction(s). It then automatically changes these products status in the database from available to sold. The other neat feature of the inventory system relates to order fulfillment. Using information pulled from the datafeed a transaction record is created. From this transaction record, the store manager can create a printable invoice to ship with the product, and check which transactions have been shipped and which still need to be.

WHEN A USER REQUESTS TO VIEW ANY PAGE

Before the page is sent the script does the following:

  • Unlocks any product locks that have expired (locks that haven’t been refreshed for over 15 minutes).
  • Renews any valid product locks for this user (items in their shopping cart) for another 15 minutes.

WHEN A USER REQUESTS TO VIEW A PRODUCT PAGE

Only products that are marked in the database as available and unlocked are diplayed.

WHEN A USER TRYS TO ADD AN ITEM TO THEIR SHOPPING CART

An AJAX (non-asynchronous) request is sent to query the database to get the current status (available, sold, or locked) of the item. This check is important as the item may have been purchased or added to somebody else’s cart since the page initially loaded for this user.

  • If the item is available for purchase and does not have a lock on it:
    • A product lock is put on the item for this user, which reserves the item for this user for 15 minutes from their last page view on the site.
    • The item is added to their shopping cart.
  • If the item has a lock on it (not yet sold, but in somebody else’s shopping cart):
    • A message telling the user the item has been reserved for a different user is diplayed.
    • The item is not added to their shopping cart.
  • If the item has been purchased by a different user since the page loaded for this user:
    • A message tellilng the user the item has been purchased is diplayed.
    • The item is not added to their shopping cart.

WHEN A USER VIEWS THEIR CART

Before the cart is diplayed (using the fc_tb_PreProcess() method) a javascript cycles through the FoxyCart JSON object. This is done to:

  • Make a list of all the items that were in the cart when its opened to compare against the items in the cart when its closed to see which (if any) items the user has removed from the cart.
  • Look for products that are one-of-a-kind and disable the quanity field in the shopping cart to ensure the user doesn’t try to buy two of a unique product.

WHEN A USER CLOSES THEIR CART

As the cart is being closed (using the fc_tb_BuildFoxyCart() method) a javascript cycles through the FoxyCart JSON object again. It compares the list of items in the cart when it was opened against when it was closed to see if there were any items removed from the cart. If there have been items removed from the cart an AJAX (asynchronous this time is okay) request is sent to unlock all the items that has been removed from the cart.

DATAFEED PROCESSING

The datafeed is used to accomplish two main tasks: update sold items status in the database and create transaction records. The transaction records also double as an order fulfillment system and are used to track which orders have shipped and to create printable invoinces to accompany the orders.