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:
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 %>
Inserindo estados brasileiros no ecommerce Spree
Acesse o banco de dados através do Phpmyadmin, e no banco de dados usado pelo seu Spree execute esta consulta:
INSERT INTO `states` (`country_id`, `abbr`, `name`) VALUES ('28', 'AC', 'Acre'), ('28', 'AL', 'Alagoas'), ('28', 'AP', 'Amapá'), ('28', 'AM', 'Amazonas'), ('28', 'BA', 'Bahia'), ('28', 'CE', 'Ceará'), ('28', 'ES', 'Espírito Santo'), ('28', 'GO', 'Goiás'), ('28', 'MA', 'Maranhão'), ('28', 'MT', 'Mato Grosso'), ('28', 'MS', 'Mato Grosso do Sul'), ('28', 'MG', 'Minas Gerais'), ('28', 'PA', 'Pará'), ('28', 'PB', 'Paraíba'), ('28', 'PR', 'Paraná'), ('28', 'PE', 'Pernambuco'), ('28', 'PI', 'Piauí'), ('28', 'RJ', 'Rio de Janeiro'), ('28', 'RN', 'Rio Grande do Norte'), ('28', 'RS', 'Rio Grande do Sul'), ('28', 'RO', 'Rondônia'), ('28', 'RR', 'Roraima'), ('28', 'SC', 'Santa Catarina'), ('28', 'SP', 'São Paulo'), ('28', 'SE', 'Sergipe'), ('28', 'TO', 'Tocantins'), ('28', 'DF', 'Distrito Federal')
Pronto, seu Spree já está pronto pra vender no Brasil.
Me recomende no Working with rails
http://www.workingwithrails.com/recommendation/new/person/14910
