Spree下拉框,用于显示变体选项值 [英] Spree Dropdown boxes for variant option values

查看:118
本文介绍了Spree下拉框,用于显示变体选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spree 3.0,并且我建立了一家销售短裤的测试店.

短裤有多种选择类型:大小,颜色,长度

我想将其在前端显示变量选项的方式从单选框更改为下拉框.

当前,Spree将选项类型显示为单选按钮:

我想更改此设置以对每种选项类型使用下拉菜单,如下所示:

我尝试了以下操作:

<%= select_tag "variant_id", options_for_select(@product.variants_and_option_values(current_currency).collect{ |v| ["#{variant_options(v)}  #{variant_price(v)}", v.id] })%>

但是它仅显示每个标记中所有选项类型的值:

我想知道将选项值分成单独的下拉菜单的最佳方法吗?

非常感谢您的协助.

解决方案

这并不像看起来那样简单,因为您将使用Spree::OptionValue记录而不是变体,并且在某些时候您将希望转换回in以便将其添加到您的购物车.可能无法进行组合和/或缺货,因此使用option_values极为不切实际.

但是,您仍然想知道如何设置以下内容:

@options = Spree::OptionValue.joins(:option_value_variants).where(spree_option_value_variants: { variant_id: @product.variant_ids }).group_by(&:option_type)

这将为您提供一个哈希,其哈希键为option_types(在您的情况下为Size,Color,Length),其值为option_values的数组.

您可以像这样轻松地将其形成为收音机:

<% @options.each do |option_type, option_values| %>
  <%= content_tag :h2, option_type.presentation %>
  <% option_values.each do |option_value| %>
    <%= radio_button_tag option_type.name, option_value.id %>
    <%= label_tag option_value.presentation %>
  <% end %>
<% end %>

或下拉菜单:

<% @options.each do |option_type, option_values| %>
  <%= content_tag :h2, option_type.presentation %>
  <%= collection_select :variants, option_type.name, option_values, :id, :presentation %>
<% end %>

在您的控制器中,您将要查找一个匹配这3个条件的变体,检查它是in_stockbackorderable还是track_inventory?false并以错误或更新的购物车进行响应:)

我希望这对您有帮助

I'm learning Spree 3.0 and I have a setup a test shop that sells shorts.

Shorts has multiple option types: Size, Color, Length

I wanted to change the way it displays the variant options on the frontend from a radio checkbox to a drop down box.

Currently, Spree displays the option types as radio buttons:

I want to change this to use drop down menus for each option type, like this:

I've tried the following:

<%= select_tag "variant_id", options_for_select(@product.variants_and_option_values(current_currency).collect{ |v| ["#{variant_options(v)}  #{variant_price(v)}", v.id] })%>

But it simply displays the values of all option types in each tag:

I wanted to know the best way to split the option values into individual dropdown menus?

Any assistance is my much appreciated, thank you.

解决方案

This is not as easy as it looks since you will be using Spree::OptionValue records instead of variants and at some point you will want to convert back to variants in order to add it to your cart. Combinations might not be possible and/or out of stock so it is highly unpractical to work with option_values.

But nonetheless, you wanted to know how so i set up the following:

@options = Spree::OptionValue.joins(:option_value_variants).where(spree_option_value_variants: { variant_id: @product.variant_ids }).group_by(&:option_type)

This will give you a hash with the keys of the hash being option_types (Size, Color, Length in your case) and the values being arrays of option_values.

You can easily form this into radios like this:

<% @options.each do |option_type, option_values| %>
  <%= content_tag :h2, option_type.presentation %>
  <% option_values.each do |option_value| %>
    <%= radio_button_tag option_type.name, option_value.id %>
    <%= label_tag option_value.presentation %>
  <% end %>
<% end %>

Or for dropdowns:

<% @options.each do |option_type, option_values| %>
  <%= content_tag :h2, option_type.presentation %>
  <%= collection_select :variants, option_type.name, option_values, :id, :presentation %>
<% end %>

And in your controller you would want to find a variant matching those 3 criteria, check if it is in_stock, backorderable or track_inventory? is false and respond with errors or an updated cart :)

I hope this helped

这篇关于Spree下拉框,用于显示变体选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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