Using acts_as_shopping_cart how do I implement basic quantity editing?
Asked Answered
D

2

0

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.

Deland answered 16/12, 2012 at 8:20 Comment(4)
Hey. What have you tried? We actually use spree as our checkout / cart and wrote a couple of APIs to connect to our core application. Have never tried acts_as_shopping cart though. The only thing we came close to doing this in was our internal ordering system and use nested fields for the products. Would that suit??Kannry
@Kannry You don't use spree for the entire storefront? Just the cart? I actually wouldn't mind using spree if I could just use it for the cart - and not the entire storefront. Is that possible? I want something that will allow a vendor to be able to specify the sizes/colors of their respective products and then allow a user to simply add them to their cart and pay for it. Think the most basic checkout process (where both a user and vendor can customize sizes, quantity & colors). I already have the products & vendors model working.Deland
No, for all of it but we adapted to suit out needs. Can you paste more view code of the cart and I'll see what I can come up with.Kannry
@Kannry Done. The current implementation of the cart is very basic.Deland
F
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

...
Fiendish answered 22/12, 2012 at 13:35 Comment(2)
I seem to have missed this comment. Thanks for this, esp. the link to the book. Really appreciate it. I think my situation will be different because I can restrict them to a standard set of sizes (i.e. either big, large, small or 10, 12, 14, etc.).Deland
I meant this answer. Thanks much.Deland
W
0

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)
Widespread answered 29/4, 2014 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.