【问题标题】:Spree, Ruby on Rails - Add to cart multiple variants of the same productSpree,Ruby on Rails - 将同一产品的多个变体添加到购物车
【发布时间】:2013-08-17 11:11:21
【问题描述】:

我希望允许客户在同一页面上添加同一产品的多个变体。现在我有一个带有单选按钮的变体列表,如何使它们复选框? 当然,将“radio_button_tag”更改为“check_box_tag”没有帮助

<% @product.variants_and_option_values(current_currency).each_with_index do |variant, index| %>
  <%= radio_button_tag "products[#{@product.id}]", variant.id, index == 0, 'data-price' => variant.price_in(current_currency).display_price %>
  <%= variant_options variant %>
<% end%>

【问题讨论】:

    标签: ruby-on-rails spree


    【解决方案1】:

    让我们创建一个示例,其中产品 ID 为 1,并且您尝试添加 ID 为 11 和 12 的变体。

    当您将 radio_button_tag 更改为 check_box_tag 时,将发布以下参数:

    products[1]:11
    products[1]:12
    quantity:1
    

    当这被 Rack 解释时,它会看到你有 2 个同名的变量,这意味着它将选择最后一个指定的变量。您的参数哈希将如下所示:

    {
      "products"=>{"1"=>"12"},
      "quantity"=>"1"
    }
    

    您可以对此进行的最简单修改是将您的复选框标记更改为:

    <%= check_box_tag "products[#{@product.id}][]", variant.id, index == 0, 'data-price' => variant.price_in(current_currency).display_price %>
    

    这将使您的哈希看起来像:

    您可以对此进行的最简单修改是将您的复选框标记更改为:

    {
      "products"=>{"1"=>["11", "12"]},
      "quantity"=>"1"
    }
    

    然后您需要修改Spree::OrderPopulator 中的此代码以处理传入的数组(而不是整数)。比如:

      from_hash[:products].each do |product_id,variant_ids|
        variant_ids.each do |variant_id|
          attempt_cart_add(variant_id, from_hash[:quantity])
        end
      end if from_hash[:products]
    

    你应该很高兴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 2017-11-19
      • 2022-08-11
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多