KonaKart Logo Adding a Shopping Cart using SOAP
Search

Abstract

This article explains techniques for adding from simple to sophisticated shopping cart functionality to web sites.   Complete code examples are provided to add shopping cart functionality to a JSP website. The example code uses KonaKart - the free java shopping cart. 

Introduction

There’s a growing trend for owners of information-only websites to add shopping cart features to capitalise on the extraordinary growth of eCommerce around the globe.  The opportunity is hard to resist when you consider that, according to a report by IMRG, Christmas shoppers in the UK took the value of online sales to over (GBP) £1b in November, which is up a staggering 44% on the same month in 2006.

This article shows how to add shopping cart functionality to a JSP website using a zero-cost, loosely-coupled, SOAP web services integration to a sophisticated shopping cart with SOAP interfaces.

How To Add a Shopping Cart?

So what is the best way to add shopping cart functionality to a website?

One solution would be to code a complete shopping cart solution from scratch and merge this into your website. This has the potential to provide a good solution but would be a huge investment in time if anything but the most trivial cart was required.    Before undertaking this development you should be aware of the extensive functionality provided by the leading shopping cart systems which would be costly to reproduce.    For example, there’s customer management, catalogue management, promotions, payment gateways, shipping gateways, and some complicated tax calculations that depend on the location of the shopper and the store.

A quicker and less-risky solution is to integrate with an existing shopping cart, deriving all the benefits of a sophisticated shopping cart solution without requiring a costly software change to your current site.   With imaginative use of stylesheets it’s straightforward for a creative web-designer to make the two sites have similar look and feel to give the impression of a more-seamless integration.

Why loosely-coupled?

Loose coupling of your current site and the shopping cart site provides a painless migration of an information-only site to an eCommerce-enabled site with minimal effort.  You could take this loose-coupling to extremes and introduce a single link from your existing store to your on-line shop and this is a solution many would be content with.   However, with very little effort, you can do much better in bringing your site to life with live product information that will encourage your visitors to buy.

The power and simplicity of SOAP enables this loosely-coupled integration allowing you to maintain your information-only site and online shop completely independently and potentially running with different technologies.

Being SOAP, the simple example described below could have been written in any language that supports SOAP calls, but JSPs have been chosen in this case:

Movie Review Example

Let us imagine, for the sake of this article, that we have a “Movie Review” website that looks like this:

Movie Review with no shopping cart

OK, it’s a simple example!   Let’s say the site contains little more than reviews of the latest movies but has attracted a lively community who might even be contributing with their own reviews.

A natural extension of this website would be to offer DVDs for sale, so rather than build all that shopping cart functionality into Movie Review, we’ll integrate with an external site.

The first step would be to add three simple links to the site to take the user directly to his current shopping cart, his account details (to review old orders), and directly to the checkout page.   These are simple HTML links which do not require SOAP:

The links were added at the top right:

Movie Review with links to Shopping Cart

Now let’s use the power of SOAP to access some information about the DVDs for sale and tempt the visitors of Movie Review to buy.   A typical set of data that might be of interest would be a list of new products.   We access this in the JSP, in just a few lines of code, as follows:

// Get the products from the KonaKart engine using a SOAP call

URL endpointUrl = new URL(kkSoapServiceUrl);

KKWSEngIf eng =
       new KKWSEngIfServiceLocator().getKKWebServiceEng(endpointUrl);

DataDescriptor dataDesc = new DataDescriptor();
dataDesc.setOffset(0); // Offset = 0
dataDesc.setLimit(3);  // We only want to get back 3 products
dataDesc.setOrderBy(DataDescConstants.ORDER_BY_DATE_ADDED);

Products products = eng.getProductsPerCategory(null,   
     /* Data Descriptor defining how many products, the offset
        and the sort order */ dataDesc,
     /* CategoryId. 3 is for DVD Movies */ 3,
     /* searchInSubCats */ true,
     /* The language id. -1 for the default store language */ -1);

Product[] prods = products.getProductArray();

Once we have the products back in the SOAP response from the shop we can arrange them how we please on the screen.

Another interesting view we could add is to show a list of best sellers.    The code is very similar, but we just use a different SOAP interface:

// Get the best sellers from the KonaKart engine using a SOAP call

URL endpointUrl = new URL(kkSoapServiceUrl);

KKWSEngIf eng =
        new KKWSEngIfServiceLocator().getKKWebServiceEng(endpointUrl);

DataDescriptor dataDesc = new DataDescriptor();
dataDesc.setOffset(0); // Offset = 0
dataDesc.setLimit(16); // We only want to get back 16 products
dataDesc.setOrderBy(DataDescConstants.ORDER_BY_TIMES_ORDERED);
dataDesc.setOrderBy_1(DataDescConstants.ORDER_BY_NAME_ASCENDING); 

Product[] prods = eng.getBestSellers(
     /* Data Descriptor defining how many products, the offset
        and the sort order */ dataDesc,
     /* CategoryId. 3 is for DVD Movies */ 3,
     /* The language id. -1 for the default store language */ -1); 

Adding these two sets of products to our Movie Review website we now get:

Movie Review with Live Product Information

Every product shown has a link that takes the user directly to the shopping cart, just a few clicks from purchase.

Utilising a rich set of SOAP APIs, as provided by KonaKart, the Movie Review website could be improved incrementally with tighter integration.

SOAP client code generation

The simple SOAP calls illustrated above are supported by code that is generated directly from the KonaKart WSDL at http://www.konakart.com/konakart/services/KKWebServiceEng?wsdl

The steps are simply:

  • Call the Apache AXIS WSDL2Java utility to produce java classes from the WSDL
  • Compile the generated classes
  • Build WAR
  • Deploy to container

All the source code and ANT build scripts used in this example are available for download in a basic development kit from http://www.konakart.com/kits/konakart-movie-review-dev-kit.zip

A WAR is also available which contains all the JSPs and everything you need to run the example in a container (such as tomcat) from http://www.konakart.com/kits/konakart-movie-review-war.zip

Feel free to try out the code in either format.  It generates code that interfaces with the live KonaKart demo web site so you don’t actually have to set up your own independent KonaKart website just to try out this example.

KonaKart – the free java shopping cart with SOAP APIs
http://www.konakart.com/