Last updated by willy 2 years ago
This page has moved. The new Grails ShoppingCart Plugin page is more up to date.
Shopping Cart Plugin
Author: Björn Wilmsmann, MetaSieveThis plugin provides generic shopping cart functionality for Grails applications.- #Installation
- #Usage
- #Adding To And Removing From The Shopping Cart
- #Alternative: Using ShoppingCartService
- #Checking Out And Emptying The Shopping Cart
- #Additional Service Methods
- #TagLib
- #Payment via PayPal
- #Persisting Shopping Cart Across Sessions
- #Further Examples
- #Plugin version history
Installation
To install the shopping cart plugin please run this command from the root folder of your project:grails install-plugin shopping-cart
Usage
In order to use the plugin you can simply have a domain class (or several domain classes) extend the new com.metasieve.shoppingcart.Shoppable entity:class Game extends com.metasieve.shoppingcart.Shoppable { … }class Book extends com.metasieve.shoppingcart.Shoppable { … }
class Content {
…
}class Download extends Content {
…
}class Movie extends Content {
…
}class NotForSale extends Content {
…
}class Content {
…
}class Download extends Content implements com.metasieve.shoppingcart.IShoppable {
com.metasieve.shoppingcart.ShoppingItem shoppingItem
…
}class Movie extends Content implements com.metasieve.shoppingcart.IShoppable {
com.metasieve.shoppingcart.ShoppingItem shoppingItem
…
}class NotForSale extends Content {
…
}addToShoppingCart() addQuantityToShoppingCart(Integer qty) removeFromShoppingCart() removeQuantityFromShoppingCart(Integer qty)
Adding To And Removing From The Shopping Cart
If, for example, you want to add an item of type Game to your shopping cart, you would simply do:def game = new Game(...)
game.addToShoppingCart()game.removeFromShoppingCart()
game.addQuantityToShoppingCart(3)
game.removeQuantityFromShoppingCart(3)
3.times {
game.addToShoppingCart()
}Alternative: Using ShoppingCartService
Instead of using the domain class methods you can also use the corresponding methods of the ShoppingCartService. To do so, you have to inject the service as usual:def shoppingCartService
def game = new Game(...)
def qty = 3
shoppingCartService.addToShoppingCart(game, qty)
shoppingCartService.removeFromShoppingCart(game, qty)shoppingCartService.addToShoppingCart(game) // qty defaults to 1
shoppingCartService.removeFromShoppingCart( game) // qty defaults to 1Checking Out And Emptying The Shopping Cart
In order to check out, all you need to do is inject the ShoppingCartService and call the checkOut(n) method of that service:def shoppingCartService
…
def checkedOutItems = shoppingCartService.checkOut()checkedOutItems.each {
println it['item']
println it['qty']
}def shoppingCartService … shoppingCartService.emptyShoppingCart()
Additional Service Methods
Finally, the ShoppingCartService also defines the following methods:setLastURL(def url) getItems() getQuantity(IShoppable product)
getItems() yields the current items of the shopping cart associated with the session.
Finally, getQuantity(IShoppable product) will return the current quantity of the product in the shopping cart associated with the session.
TagLib
A tag for easily iterating over the current items in a shopping cart is provided, too:<table>
<sc:each>
<tr>
<td>
${com.metasieve.shoppingcart.Shoppable.findByShoppingItem(it['item'])}
</td>
<td>
${it['qty']}
</td>
…
</tr>
</sc:each>
</table>Payment via PayPal
Using the Grails PayPal plugin you can easily add payment functionality to your shopping cart as well:import org.grails.paypal.Paymentclass ShoppingCartPayment { com.metasieve.shoppingcart.ShoppingCart shoppingCart Payment payment Boolean payed = false def commit() { if (!payed) { shoppingCart.items.each { item -> … } // detach shopping cart shoppingCart.checkedOut = true shoppingCart.save() payed = true save() } } }
Persisting Shopping Cart Across Sessions
By append WithSessionID to each method name you can also use an additional session ID attribute when adding to or removing from a shopping cart, which comes in handy if you want to persist a shopping cart across browser sessions:def qty = params.quantity def previousSessionID = Session.findByCookieID(cookieID)...addToShoppingCartWithSessionID(previousSessionID) removeFromShoppingCartWithSessionID(previousSessionID) addQuantityToShoppingCartWithSessionID(qty, previousSessionID) removeQuantityFromShoppingCartWithSessionID(qty, previousSessionID)
def product = Product.get(params.id) def qty = params.quantity def previousSessionID = Session.findByCookieID(cookieID) def shoppingCart = shoppingCartService.getShoppingCart(previousSessionID)...shoppingCartService.addToShoppingCart(product, qty, shoppingCart) shoppingCartService.removeFromShoppingCart(product, qty, shoppingCart) shoppingCartService.addToShoppingCart(product, shoppingCart) shoppingCartService.removeFromShoppingCart(product, shoppingCart) setLastURL(url, shoppingCart) def items = getItems(shoppingCart) def someQty = getQuantity(product, shoppingCart)
Further Examples
For further examples please have a look at the source code in:- $PLUGIN_DIR/shopping-cart-$VERSION/grails-app/controllers/com/metasieve/shoppingcart/TestShoppingCartController.groovy
- $PLUGIN_DIR/shopping-cart-$VERSION/grails-app/views/testShoppingCart/_shoppingCartContent.gsp
- $PLUGIN_DIR/shopping-cart-$VERSION/grails-app/views/testShoppingCart/show.gsp



