Spree commerce - Como adicionar mais de uma Variação de um produto de uma única vez ao carrinho
Precisei para um projeto de Ecommerce usando o Spree, que para cada Variação de produto houvesse um campo com a quantidade e também que se pudesse adicionar mais de um produto por vez ao carrinho.
Para conseguir isso precisei mudar dois arquivos.
No orders_controller.erb :
create.after do params[:quantities].each do |variant_id,quantity| @order.add_variant(Variant.find(variant_id.to_i), quantity.to_i) if quantity.to_i > 0 end params[:products].each do |product_id,variant_id| quantity = params[:quantity].to_i if !params[:quantity].is_a?(Array) quantity = params[:quantity][variant_id].to_i if params[:quantity].is_a?(Array) @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0 end if params[:products] params[:variants].each do |variant_id, quantity| quantity = quantity.to_i @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0 end if params[:variants] @order.save # store order token in the session session[:order_token] = @order.token end
E _cart_form.html.erb :
<% form_for :order, :url => orders_url do |f| %> <% if product_price(@product) %> <p class="prices"> <%= t("price") %> <span class="price selling"><%= product_price(@product) %></span> </p> <% end %> <% if @product.variants? %> <div id="product-variants"> <ul> <li> <span>Qtde:</span> </li> <% @product.variants.active.each_with_index do |v,index| next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products]) %> <li> <%= variant_options v %> <%= text_field_tag (@product.variants? ? "quantities[#{v.id}]" : "variants[#{@product.variant.id}]"), 0, :class => "title", :size => 3 %> </li> <% end %> </ul> </div> <% end%> <br style="clear:both" /> <br style="clear:both" /> <% if @product.has_stock? || Spree::Config[:allow_backorders] %> <p> <button type='submit' class='large primary'> <%= image_tag('/images/add-to-cart.png') + t('add_to_cart') %> </button> </p> <% else %> <%= content_tag('strong', t('out_of_stock')) %> <% end %> <% end %>
Com isso já é possível que o cliente adicione para cada variação de produto, uma quantidade específica e possa também adiocioná-los de uma única vez.
Também devo creditar o Leandro pela solução, ele deu uma força na hora de finalizar.
No final a tela fica assim:
VimMate - Addon para Vim
VimMate is a graphical add-on to Vim with IDE-like features: it does more than the plain Vim while still being lightweight. Even with the additional features, it stays out of the way for it's main task: editing files with Vim. VimMate adds functionality to Vim by embedding Vim GTK GUI (gVim) within VimMate.
Veja o vídeo: http://vimmate.rubyforge.org/VimMate-0.6.0-features.swf
Página do projeto no Rubyforge: http://vimmate.rubyforge.org/
Spree commerce - Vendendo as "Variações de um Produto"
Como vender uma varição de produto apenas? Sendo que cada um tenha sua própria quantidade.
Essa foi uma solução que bolei.
<% form_for :order, :url => orders_url do |f| %> <% unless @product.variants? %> <% if product_price(@product) %> <p class="prices"> <%= t("price") %> <span class="price selling"><%= product_price(@product) %></span> </p> <% end %> <% end %> <% if @product.variants? %> <div id="product-variants"> <ul> <% 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> <!-- --> <% form_for :order, :url => orders_url do |f| %> <p class="prices"> <%= variant_options v %> - <span class="price selling"><%= product_price v %></span><br /> <p>Qtde: <%= text_field_tag (@product.variants? ? :quantity : "variants[#{@product.variant.id}]"), 1, :class => "title", :size => 3 %> <%= hidden_field_tag "products[#{@product.id}]", v.id %> <button type='submit' class='large primary'> <%= image_tag('/images/add-to-cart.png') + t('add_to_cart') %> </button> </p> <!-- --> <!-- %= radio_button_tag "products[#{@product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders] % --> <% end %> </label> </li> <% end%> </ul> </div> <% end%> <% unless @product.variants? %> <% if @product.has_stock? || Spree::Config[:allow_backorders] %> <p>Qtde: <%= text_field_tag (@product.variants? ? :quantity : "variants[#{@product.variant.id}]"), 1, :class => "title", :size => 3 %> <button type='submit' class='large primary'> <%= image_tag('/images/add-to-cart.png') + t('add_to_cart') %> </button> </p> <% else %> <%= content_tag('strong', t('out_of_stock')) %> <% end %> <% end %> <% end %>
Colocando as variantes dentro de um select:
<% form_for :order, :url => orders_url do |f| %> <% if product_price(@product) %> <p class="prices"> <%= t("price") %> <span class="price selling"><%= product_price(@product) %></span> </p> <% end %> <!-- --> <div id="product-variants"> <select name='<%= "products[#{@product.id}]" %>'> <option value="0" selected="selected" disabled="disabled">Escolha uma opção</option> <% if @product.variants? %> <% 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 %> <option value='<%= v.id %>'> <%= variant_options v %> - <% if variant_price_diff v %><%= variant_price_diff v %><% end %> </option> <!-- --> <!-- %= options_for_select "products[#{@product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders] % --> <% end %> </select> </div> <% end%> <% if @product.has_stock? || Spree::Config[:allow_backorders] %> <p>Qtde: <%= text_field_tag (@product.variants? ? :quantity : "variants[#{@product.variant.id}]"), 1, :class => "title", :size => 3 %> <button type='submit' class='large primary'> <%= image_tag('/images/add-to-cart.png') + t('add_to_cart') %> </button> </p> <% else %> <%= content_tag('strong', t('out_of_stock')) %> <% end %> <% end %>
Plugin para Rails Facebox com Jquery
Facebox v1.0 ==================== http://github.com/albertobraschi/facebox/tree/master Se você precisa de uma solução leve de janela modal e com jquery, este plugin é para você. Configurando e usando: --First things first-- Adicione o css do facebox: <%= stylesheet_link_tag 'lightbox' %> Caso você ainda não tenha carregado os javascripts: <%= javascript_include_tag :defaults %> Uma última coisa: Dica: O melhor lugar pra se por javascript é logo antes de tag
