【发布时间】:2013-08-06 09:19:29
【问题描述】:
我在我的应用程序中使用 spree 2.0.0 stable。在产品展示页面上,所有变体都显示为单选按钮。我只想在下拉列表中显示它们。对此有什么想法吗?
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 spree
我在我的应用程序中使用 spree 2.0.0 stable。在产品展示页面上,所有变体都显示为单选按钮。我只想在下拉列表中显示它们。对此有什么想法吗?
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 spree
注意:此解决方案实施 Spree “模板替换方法”,尤其是当您在应用程序设计中进行大量设计更改或使用自定义设计时。看这里
http://guides.spreecommerce.com/developer/view.html
如果您使用 Spree 商店的默认设计或细微更改,请使用“deface”方法。
转到:
app/views/spree/products/_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.)
希望这对你也有用。
谢谢
【讨论】:
选择标签似乎还需要一个 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 文件的样子。
<!-- replace_contents "[data-hook='inside_product_cart_form'] #product-variants, #inside-product-cart-form[data-hook] #product-variants" -->
<h6 class="product-section-title"><%= Spree.t(:licenses) %></h6>
<%= 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 => '
<h3 class="product-section-title"><%= Spree.t(:variants) %></h3>
<%= select_tag "variant_id",
options_for_select(@product.variants_and_option_values(current_currency).collect{ |v| ["#{variant_options(v)} #{variant_price(v)}", v.id] })%>
');
【讨论】: