狂欢如何在下载Ruby on Rails中显示变体?

我在我的应用程序中使用spree 2.0.0 stable。 在产品展示页面上,所有变体都显示为单选按钮。 我只想在下拉列表中显示它们。 有什么想法吗?

谢谢。

注意:当您在应用程序设计中使用大量设计更改或使用自定义设计时,此解决方案特别实现Spree“模板替换方法”。 看这里

http://guides.spreecommerce.com/developer/view.html

否则,如果您使用Spree商店的默认设计或微小更改,请使用“deface”方法。

去:

应用程序/视图/大礼包/产品/ _cart.html.erb。 并在购物车内部表格中写下以下内容。

<%= select_tag "products[#{@product.id}]", options_for_select(@product.variants_and_option_values(current_currency).collect{|v| ["#{variant_options(v)} #{variant_price(v)}", v.id]})%> #(if you don't have this file(app/views/spree/products/_cart_form.html.erb) go to github spree2.0.0 branch and use it in your product.) 

希望这也适合你。

谢谢

select标签似乎也需要ID为“variant_id”,否则您将在订单填充操作中收到404错误。

从Spree 2.2.0.beta开始(可能更早),您应该使用包含的Deface gem进行此修改,而不是直接编辑核心文件。

要替换spree前端视图文件app/views/spree/products/_cart_form.html.erb代码的内容(注意自Spree v2.0以来名称已更改):

app/overrides/spree/products/_cart_form/创建一个文件夹,并添加一个带有您选择名称的.deface文件,例如。 variant_dropdown.html.erb.deface在这种情况下,由于替换代码包含动态ruby代码,因此.erb是必需的。

然后,在此文件的内容中,选择您尝试编辑核心的代码,并将其替换为您自己的自定义代码。 这是我的.deface文件的样子。

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

重点是Spree的任何未来更新都会覆盖您的代码或要求您每次都手动重写代码。 这会尝试通过挂钩将继续更新的data选择器来保护您的更改。

这就是我为spree 3.0所做的。 这被放在一个文件\ app \ overrides \ use_drop_down_for_variants.rb中

 Deface::Override.new(:virtual_path => 'spree/products/_cart_form', :name => 'use_drop_down_for_product_variants', :replace_contents => '[id="product-variants"]', :text => ' 

<%= Spree.t(:variants) %>

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