Passing quantity in a shopping cart


#1

I am using this line on my index.rhtml page to capture the quantity
required of a particular item for a basic shopping cart app:

<%= text_field ‘line_item’, ‘quantity’, {:size=>3} %>

but my next screen (a cart screen) doesn’t reflect what was entered in
that text field when the item is added to the cart. That is,

<%= item.quantity %>

shows 0 no matter what is entered on index.rhtml (I removed the code
that was setting it to 1 in the line_item model.)

How should I pass the quantity the client wants to order to the shopping
cart?

My db table is named line_items and has an integer field named
quantity… this is basically an exercise to extend the cart app in Agile

TIA…


#2

What does your code look like that populates the line items from the
form
data?


#3

Hunter’s Lists wrote:

What does your code look like that populates the line items from the
form
data?

class LineItem < ActiveRecord::Base

belongs_to :product
belongs_to :order
def self.for_product(product)
item = self.new
item.product = product
item.unit_price = product.price
item
end
end

This is the controller for the line_item… I couldn’t define
item.quantity since quantity is already a field in the line_items table.
Maybe I’ve got quantity in the wrong table? My other tables are
products, orders, and users. There is also a qty_available in products
but that reflects stock level and not the quantity that is being
ordered.


#4

This looks like the model, not the controller.

You should have code in your controller that basically takes the stuff
from
the form and puts it in an instance of LineItem.


#5

Hunter’s Lists wrote:

This looks like the model, not the controller.

You should have code in your controller that basically takes the stuff
from
the form and puts it in an instance of LineItem.

The code in my controller looks like this:

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
rescue
logger.error(“Attempt to access invalid product #{params[:id]}”)
redirect_to_index(‘Invalid Product’)
end

Would I add a line that says @cart.add_quantity(line_item) ?


#6

Hunter’s Lists wrote:

This code looks up a product based on the passed in id, finds the cart,
adds
the product that it found to the cart and redirects.

Nowhere in this code is it pulling anything from any form (except for
the
id).

Based on this, it seems strange that this would be fired by submitting a
form. This looks like an action that would fire attached to a hyperlink
on a
screen listing products, to add them to your cart.

Sorry but I am very new to Ruby and OO programming general. I looked up
forms and it said that it could be called or just embedded like I have
it in my index.rhtml file: <%= text_field ‘line_item’, ‘quantity’,
{:size=>3} %>

With that, I am guessing I need to add a model to my cart.rb that
defines a method for add_quantity and then call that method in my store
controller at the same time I call the add_to_cart since that only adds
products at this point.

Does that sound right or am I way off?


#7

This code looks up a product based on the passed in id, finds the cart,
adds
the product that it found to the cart and redirects.

Nowhere in this code is it pulling anything from any form (except for
the
id).

Based on this, it seems strange that this would be fired by submitting a
form. This looks like an action that would fire attached to a hyperlink
on a
screen listing products, to add them to your cart.


#8

I made a boo-boo, ignore that line that says item.unit_price =
product[price_field], just remove it :wink:

Thats a little something I’m working on for custom prices for
different customer types.

Shawn


#9

Ok, so maybe I should proof read code that I’m working on before I post
it.
come on, flame me you know you want to. :wink:

Corrected Code Here:

class ShipmentItem < ActiveRecord::Base
set_table_name “transaction_items”

belongs_to :product
belongs_to :shipment, :foreign_key => "shipment_id", :dependent => 

true
acts_as_list :scope => :shipment

def self.for_product(product,qty=1,price_field="price")
    item = self.new
    item.sku = product.sku
    item.quantity = qty
    item.product = product
    item.unit_price = product.price
    item.recalc_subtotal
    item
end

def quantity=(value)
    write_attribute("quantity",value)
    self.recalc_subtotal
end

def price=(value)
    write_attribute("unit_price",value)
    self.recalc_subtotal
end

def recalc_subtotal
    if @quantity.nil? or @unit_price.nil?
        write_attribute(:subtotal,0)
    else
        write_attribute(:subtotal,@quantity * @unit_price)
    end
end

end


#10

Vince,

I exetended the line_item example like this:
the important thing is the qty=1 in the self.for_product definition.
when you call it you do this:
shipment <<
ShipmentItem.for_product(@product,@params[:lineitem][“quantity”])

or whatever variation works for you.

Shawn

class ShipmentItem < ActiveRecord::Base
set_table_name “transaction_items”

belongs_to :product
belongs_to :shipment, :foreign_key => "shipment_id", :dependent => 

true
acts_as_list :scope => :shipment

def self.for_product(product,qty=1)
    item = self.new
    item.sku = product.sku
    item.quantity = qty
    item.product = product
    item.unit_price = product[price_field]
    item.unit_price = product.price
    item.recalc_subtotal
    item
end

def quantity=(value)
    write_attribute("quantity",value)
    self.recalc_subtotal
end

def price=(value)
    write_attribute("price",value)
    self.recalc_subtotal
end

def recalc_subtotal
    if @qty.nil? or @unit_price.nil?
        write_attribute(:subtotal,0)
    else
        write_attribute(:subtotal,@qty * @unit_price)
    end
end

end