82138

Add multiple items to a cart at the same time

I have the following relations :

Class CartItem belongs_to :cart belongs_to :product Class Product (<=> category) has_many :cart_items Class Cart has_many :cart_items

When someone adds a product to his cart, it creates a line in the CartItem table with cart.id and product.id. I can add et remove a product to a cart on the edit page, it works.

But now I would like to add or remove more than one product to a cart at the same time. I just need an input number field when the customer could put the number we want to add/remove. However, I don't manage to do it because in the edit form, if i put a field called for example "number" (for each product) an error will obviously appear because there is no attribute "number". I should probably add a "field_tag" but how can it work ? Thanks in advance

Answer1:

I work on an eCommerce gem written with Rails called Spree. Here's how we've solved that problem.

We have 4 models: Variant, Product, Order and LineItem. These form the basics of our ordering system. A variant can be considered like a "mutation" of a product. Say you have a TShirt that could come in Red, Green or Blue. The Product is the TShirt, while the colours are the variants.

Variants are linked to Orders by way of LineItems. The LineItem object stores: a variant_id, a quantity and the current price of the variant, just in case that changes later. You don't want prices changing on users unexpectedly!

The logic for adding an item to the cart is simply a form with a variant_id and a quantity field on the product's page. Here's Spree's version. The controller's action that deals with that form basically takes the variant_id and quantity, and does this:

<ol> <li>Checks if an order already exists</li> <li>If an order doesn't exist, creates one</li> <li>Creates a new line item on the order with the quantity and variant_id specified, and stores the price.</li> </ol>

Spree's voodoo around this is a little more complex as we care about inventory levels and so on, but that's the basic gist of it.

When the user goes to view their cart, we present them with a form with all the line items and a quantity numeric field. That form looks like this:

checkout http://f.cl.ly/items/3P3t3o1F283a1e0Z3e0C/Screen%20Shot%202013-07-11%20at%208.39.20%20AM.png

The code to produce that form is in three files: orders/edit.html.erb, orders/_form.html.erb and orders/_line_item.html.erb

That form works by submitting to OrdersController#update, and due to the fields in the form being nested attributes, the entire order and its line items are updated suitably.

I hope this helps you!

Recommend

  • Rails Deface on input erb element, cannot override it correctly
  • Spree::OrderPopulator accessing outside of store
  • Spree: intersection between taxons
  • Where is the CheckoutController#edit in spree?
  • Product and product template in Odoo 10
  • Spree, Ruby on Rails - Add to cart multiple variants of the same product
  • Installing SSL on AWS EC2 Bitnami Mean Stack
  • Shopify API CARTS - Changing line_item line_price for price Override
  • Typecasting `this` of a base class template to its derived class
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Error while using PDF Kit
  • Client side validation mvc dropdown
  • Not able to display correct data in table -AngularJS
  • Should a web service response include empty values?
  • Pass array from parent template to child template
  • Django model for a Postgres view
  • Jquery Mobile pageLoading() Method how does it work?
  • Two Tables Serving as one Model in Rails
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • How to override value that appears in a dropdown in the rails_admin gem
  • Combining two different ActiveRecord collections into one
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Loading .coffee files via a view in Rails
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Inline R code in YAML for rmarkdown doesn't run
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Ensure fsync did its job
  • Breeze - Deleted Items nav properties bug
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • javaw.exe and eclipse startup problems
  • HTML download movie download link
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • need help with bizarre java.net.HttpURLConnection behavior
  • How can i traverse a binary tree from right to left in java?
  • Converting MP3 duration time