Hayden
Hayden

Reputation: 43

Shopping cart like rails app

I am a new rails developer building a site that is a delivery service for dry cleaning.

The user is going to select how many items they want picked up, pants, shirts ect.

Here is my table:

create_table "products", force: :cascade do |t|
  t.string   "name"
  t.decimal  "price"
  t.datetime "created_at",   null: false
  t.datetime "updated_at",   null: false
  t.string   "product_type"
  t.text     "image"
end

All of the products are saved in the database. I want each user to be able to select the quantity and type of clothing and quantity on the order page.

How would I go about adding order total and add the transaction to their recent order history of all the items selected?

I believe I will need jquery to update the database selected at all times once the item is checked. Add selected items to an order database if selected and sum up order total for payment?

Sorry, noob question, thanks in advance!

Upvotes: 2

Views: 805

Answers (1)

the12
the12

Reputation: 2425

I actually built an e-commerce relatively recently, so here's what I did. I created separate models called Cart and CartItem. You can just switch the model Phone to Product (or whatever the model used to store information related to the product (i.e.: price, quantity available, etc.)

class CartItem < ApplicationRecord
  belongs_to :cart, {:optional => true}
  belongs_to :phone, {:optional => true}
end

class Cart < ApplicationRecord
  has_one :user
  has_many :cart_items
end

class Phone < ApplicationRecord
  has_many :cart_items
end

The Cart model has no attributes. Its main purpose is just to hold different cart items.

The CartItem model has a foreign key of which cart it belongs to (cart_id) as well as a foreign key on which phone it has (phone_id) (you can switch it to clothing). Each cart item can only have one phone, and a corresponding quantity for that phone . Thus you can cycle through @user.cart.cart_items and get each clothing item as well as the quantity associated with each clothing.

Attributes required for the model CartItem:

t.integer  "cart_id"
t.integer  "phone_id"
t.integer  "quantity_sold" 

Thus using these models, and their respective attributes, you can calculate totals by having a calculate_total method that gets run everytime you go to the cart page:

def calculate_totals
  phones_array = []
  if @user.cart.cart_items.present?
    @user.cart.cart_items.each do |item|
      phone_total = item.phone.price * item.quantity_sold
      phones_array << phone_total
    end
    @subtotal = phones_array.inject{|memo,n| memo + n}
    @tax_total = @subtotal * 0.13
    @total = @subtotal + @tax_total
  end
end

def cart
  calculate_totals
end

Note it will only run if @user has cart items . As you can see for the controller method calculate_totals, you basically iterate through your cart items, grab the price of each of the items and multiply it by the quantity sold. The result then gets stored in the phones_array variable.

To calculate @subtotal, you're simply adding all the elements of phones_array with .inject. You get @tax_total by multiplying @subtotal by the tax rate and to calculate @total you're just adding @subtotal by @tax_total.

Upvotes: 1

Related Questions