同时将多个物品添加到购物车 [英] Add multiple items to a cart at the same time

查看:125
本文介绍了同时将多个物品添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下关系:

  Class CartItem 
所属类别:购物车
所属类别:产品

类别产品(< =>类别)
has_many:cart_items


类别购物车
has_many:cart_items

某人将产品添加到购物车时,它将在CartItem表中创建带有cart.id和product.id的行。
我可以在编辑页面的购物车中添加或删除产品,它可以正常工作。



但是现在我想添加或删除一个以上的产品同时放入购物车。当客户可以放入我们要添加/删除的号码时,我只需要一个输入号码字段。但是,我无法做到这一点,因为在编辑表单中,如果我输入一个名为 number的字段(对于每种产品),则显然会出现错误,因为没有属性 number。我可能应该添加一个 field_tag,但它如何工作?在此先感谢

解决方案

我正在研究用Rails编写的名为



生成该表单的代码分为三部分文件: orders / edit.html.erb orders / _form.html.erb orders / _line_item.html.erb


该表单通过提交给 OrdersController#update 起作用,并且由于表单中的字段是嵌套属性,整个订单及其订单项会适当更新。


我希望这对您有所帮助!


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

解决方案

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:

  1. Checks if an order already exists
  2. If an order doesn't exist, creates one
  3. Creates a new line item on the order with the quantity and variant_id specified, and stores the price.

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:

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!

这篇关于同时将多个物品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆