Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Simple Shopping Cart implementation

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 acts_as_shopping_cart.gemspec
README.md

acts_as_shopping_cart

A simple shopping cart implementation. Still on Alpha, use it at your own risk.

Install

Rails 3

Include it in your Gemfile

gem 'acts_as_shopping_cart', :git => "git@github.com:crowdint/acts_as_shopping_cart.git"

And run bundler

bundle install

Usage

You need two models, one to hold the Shopping Carts and another to hold the Items

You can use any name for the models, you just have to let each model know about each other.

Examples

For the Shopping Cart:

class Cart < ActiveRecord::Base
  acts_as_shopping_cart_using :cart_item
end

For the items:

class CartItem < ActiveRecord::Base
  acts_as_shopping_cart_item_for :cart
end

or, if you want to use convention over configuration, make sure your models are called ShoppingCart and ShoppingCartItems, then just use the shortcuts:

class ShoppingCart < ActiveRecord::Base
  acts_as_shopping_cart
end

class ShoppingCartItem < ActiveRecord::Base
  acts_as_shopping_cart_item
end

Migrations

In order for this to work, the Shopping Cart Item model should have the following fields:

create_table :cart_items do |t|
    t.integer :cart_id            # Holds the cart id, for polymorphism
    t.string  :cart_type          # Holds the type of the cart, for polymorphism
    t.integer :item_id            # Holds the object id
    t.string  :item_type          # Holds the type of the object, for polymorphism
    t.integer :quantity           # Holds the quantity of the object
    t.decimal :price, :scale => 2 # Holds the price of the item
end

Add Items

To add an item to the cart you use the add method. You have to send the object and the price of the object as parameters.

So, if you had a Product class, you would do something like this:

@cart = Cart.create
@product = Product.find(1)

@cart.add(@product, 99.99)

In the case where your product has a price field you could do something like:

@cart.add(@product, @product.price)

I tried to make it independent to the models in case you calculate discounts, sale prices or anything customized.

You can include a quantity parameter too.

@cart.add(@product, 99.99, 5)

In that case, you would add 5 of the same products to the shopping cart. If you don't specify the quantity 1 will be assumed.

Remove Items

To remove an item from the cart you can use the remove method. You just have to send the object and the quantity you want to remove.

@cart.remove(@product, 1)

Total

You can find out about the total using the total method:

@cart.total # => 99.99

Total unique items

You can find out how many unique items you have on your cart using the total_unique_items method.

So, if you had something like:

@cart.add(@product, 99.99, 5)

Then,

@cart.total_unique_items # => 5

TODO

  • Finish this document
  • Test it on Rails 2
  • Some more useful methods, like @cart.quantity_for(@product), @cart.price_for(@product)
Something went wrong with that request. Please try again.