Spree - Adding multiple variants to cart on the sa

2019-08-05 23:33发布

I'm using Spree for a ecommerece site, each product that will be sold comes in a large number of colors and most customers will want to purchase different colors of the same product (e.g. variants). I want customers to be allowed to add multiple variants of the same product on the same page, I currently have a list of variants with a radio button that allows a choice of variant and quantity to be purchased. Instead I just want a number of quantity boxes that default to zero so the customer can simply add the number they want to each variant and click add to cart. After looking at the orders controller I came up with this

 <% has_checked = false
                  @product.variants.active.each_with_index do |v,index|
                    next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products])
                    checked = !has_checked && (v.in_stock || Spree::Config[:allow_backorders])
                    has_checked = true if checked %>
                    <li>

                      <label for="<%= ['products', @product.id, v.id].join('_') %>">
                        <span class="variant-description">
                          <%= variant_options v %> <%= text_field_tag (@product.has_variants? ? :quantity : "variants[#{@product.master.id}]"),1, :class => "title", :size => 3 %>
                        </span>
                        <% if variant_price_diff v %>
                          <span class="price diff"><%= variant_price_diff v %></span>
                        <% end %>
                      </label>
                    </li>
                  <% end%>

It works in the sense that it shows what I want, a list of variants and quantity boxes, but whenever I add quantities and add them to the cart it just defaults to the last item on the list and the quantity in the last quantity box. I've tried a few things but none have correctly, Does anyone know how I would go about doing this?

1条回答
三岁会撩人
2楼-- · 2019-08-05 23:56

Neeeeeeeevermind, read the model for order_populate, didn't realize it was using two different hash models from products/variants, I was trying to pass the hash products, which caused the errors because it wasn't expecting a hash with the products hash, instead used a variants hash, using the code

<%= text_field_tag "variants[#{v.id}]",0, :class => "title", :size => 3, :disabled => !v.in_stock && !Spree::Config[:allow_backorders] %>

Hope this helps other people!

查看更多
登录 后发表回答