Using acts_as_shopping_cart how do I implement bas

2019-01-29 10:47发布

问题:

The acts_as_shopping_cart gem requires two models - Shopping Cart and Shopping Cart Item.

The attributes it allows you to access like this for the item:

<td><%= shopping_cart_item.item.name %></td>
<td><%= shopping_cart_item.item.price %></td>
<td><%= shopping_cart_item.quantity %></td>

But I want to allow users to change the quantity - from say a drop-down menu (so a select tag from a collection).

But I am not quite sure how to approach that.

I would also like to add other attributes to my item - like a size of an item, color, etc.

I would like my store owner to be able to specify those things (i.e. size, color, etc.).

How do I do that within the confines of acts_as_shopping_cart?

Thanks.

Edit 1:

Or if someone has a better suggestion for another shopping cart solution that will allow me to do basic checkout, I would appreciate that too.

Edit 2

views/shopping_cart/show.html.erb

<h1>Shopping Cart</h1>

<table class="table table-striped">
  <thead>
        <tr>
            <td>Item</td>
            <td>Price</td>
            <td>Quantity</td>
        </tr>
    </thead>
    <tbody>
        <tr>  
      <%= render :partial => 'shopping_cart_item', :collection => @shopping_cart.shopping_cart_items %>
    </tr>
  </tbody>  
</table>

<div>
    <p>SubTotal: <%= number_to_currency @shopping_cart.subtotal %></p>      
</div>
<div>
    <p>Taxes: <%= number_to_currency @shopping_cart.taxes %></p>
</div>
<div>
    <p>Total: <%= number_to_currency @shopping_cart.total %></p>
</div>

_shopping_cart_item.html.erb partial looks like this:

<td><%= shopping_cart_item.item.name %></td>
<td><%= shopping_cart_item.item.price %></td>
<td><%= shopping_cart_item.quantity %></td>

Very very basic shopping cart - but not sure how to move from this to an actual shopping cart with quantity, size, etc.

回答1:

Quantity:

Actually, I don't know acts_as_shopping cart and what exactly it do. But anyway look at the 'standard' realization in Agile Web Development with Rails (4ed), page 117. You can just steal those cart to your application, or whole the application. I think this book is about you. :)

Product properties:

It is VERY important to clarify the problem from the beginning and its potential complications due to the demands of the owner. So you do not have trouble link there.

Any 'attributes' have to be known and classified carefully. Let say, color is a simple anywhere (red is red in america, africa or europe), but the size - is not. Size of what? Cloth size depends on region dimension, culture, cloth type etc and can be just one value ("M", "S" or "46") and depends on product origin... Size of 3D is a 3 numerics (can be inches, cm, m) and depends on region of sale.

So, due to OOP thinking, you have to write every product 'attribute' in a stand-alone ActiveRecord model (which has_many product_items) but you have to know exactly HOW you will be able to search them as fast as possible.

At the finish i think you accomplish something like that.

size = Size.new(:origin => :us, :value => "m")
size.origin
#=> "us"
size.value
#=> "m"
size.with_analogs # model knows how to translate sizes between each other
#=> [<self>, <#Size @origin: "europe", @value: 46>, <#Size. @origin: "europe", @value: 45.5>, <#Size @origin: "australia", @value: ...>]

So despite the fact that the product item has (actually belongs_to) only one particular size, you can find a similar size of similar goods like that:

product = Product.find(1)
Product.where(:name => "%#{product.name}", :size => {:id => [product.size.with_analogs.map(&:id)]})

Or you can store in the database common measurement learning STI how to translate them, but it little bit complicated.

class Size
class EuropeSize < Size     # STI
class UsSize < Size         # STI
class AustraliaSize < Size  # STI

...


回答2:

Here Is how I add/edit the quantity

  1. On the shop page, I create a new shopping cart and store its id in the session
  2. Adding items to the shopping cart is handled using Ajax calls that will return the new cart items html to update a div in the header
  3. On the checkout page, there is quantity edit textbox for each item.
  4. On change of the quantity, I make an ajax call with both the new quantity and product_id (you could do it using cart_item_id)