【问题标题】:What is the right way to add product options user has selected to cart?将用户选择的产品选项添加到购物车的正确方法是什么?
【发布时间】:2017-03-09 16:40:18
【问题描述】:

我的架构如下所示:

Combo      |    Product     |    Variant        |   OptionType    |   OptionValue
  id       |      id        |      id, name     |     id          |     id
  name     |      name      |      price        |     name        |     name
  dicount  |      combo_id  |      product_id   |     variant_id  |     option_type_id
                            |      price        |                 |     price
________________________________________________________________________________________

                 Cart       |    CartItem
                   id       |      id
                   user_id  |      cart_id
                            |      purchasable_id
                            |      purchasable_type
                            |      quantity

                 //purchasble: polymorphic, Combo and Product are purchasable

举个例子会更容易理解

Product: Some Shirt, Some Pants, SomeOther Shirt, etc.,
Combo: Shirt + Pants combo (discount: $20)

For the product 'Some Shirt',
Variants: Small ($30), Medium ($35), Large ($40)
OptionTypes: Print, Add-ons
OptionValues for 'Print': CustomLogo ($10), FullSizePrint ($20)
OptionValues for 'Add-ons': Frills ($1), InnerPockets ($2)

当我将某些东西添加到购物车时,会创建一个 Cart,并为刚刚创建的新 Cart 创建一个新的 CartItem。我的问题是

如何获取用户选择的变体和选项?

*注意:我不想在 json 中发送 optionValueIds 和 variantId 的数组并计算并将价格复制到 CartItem:我真的需要那些 @ 987654330@s 和 variantIds 持久化,即在数据库中。

现在,CartItem 只包含Productid,显然Product.Variants 会给出所有可能的变体,OptionTypesOptionValues 也是如此

我浅浅地观察了 Spree 是如何做到这一点的:CartItemvariant_id 而不是 product_id。换句话说,他们将Variants 添加到购物车并获得产品对象:via 变体。好吧,它们消除了单独发送variantId 的需要,但我仍然不知道它们是如何获得的,以及它们存储用户选择的optionValueIds 数组的位置。我也看过这个SO Post,但它没有任何答案,而且它更具体到 Spree

我想到了什么?

  • CartItem 中添加另一列options_chosen,并发送用户选择的optionValueIds 序列化数组; 为什么会不好? 首先,它不适用于Combo。其次,原子性丢失了。
  • 在名为 CartVariant 的新模型中添加列 options_chosen,并引用 Variant 并将 CartVariants Spree 方式添加到购物车。 我的想法:我不知道这是否是正确的方法。获取产品对象应该类似于CartItem.CartVariant.Variant.Product,我不知道如何让Combos 工作。顺便说一句,即使在这里原子性也丢失了;

【问题讨论】:

  • 嗨,Zeke,我现在正在努力解决这个确切的问题。你想通了吗?我有一个产品页面,其中包含两个可供客户选择的变体。我想了几种方法来做到这一点,但他们都觉得自己像个黑客。

标签: ruby-on-rails database-schema shopping-cart


【解决方案1】:

我将 product_id 作为隐藏字段包含在产品页面上,然后创建单选输入按钮来选择特定变体。当我提交表单时,我将 product_id 和 variant_id 参数传递给 LineItems 控制器,在那里我使用它们来构建我的 line_item。那有意义吗?我已经为此工作了几个小时,终于成功了!

<%= form_tag line_items_path, method: :post, :id => "print_form" do %>
  <%= hidden_field_tag :product_id, @product.id %>

  <% @product.variants.each do |variant| %>
    <label>
      <input type="radio" name="variant_id" value="<%= variant.id %>" /><%= variant.name %>
    </label>
  <% end %>

【讨论】:

  • 绝对没有问题找到用户添加到cartvariant,因为我没有添加products,而是variants到购物车(我可以识别产品像 Spree 那样使用variant.product)。这样,我什至不需要将product_ids 发送到服务器。
  • 真正的问题在于发送OptionTypesOptionValues。更重要的是,当涉及到Combos 时,它们嵌套在Variants 下,从而阻止我们将它们直接链接到CartItemLineItem
  • 对,但是为什么不添加 OptionTypes 和 OptionValues,就像我将 Variant 选择与产品一起发送一样?它只是在我的购物车中变成了多个相关的 LineItems。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
相关资源
最近更新 更多