diff --git a/app/assets/stylesheets/config/_bootstrap_variables.scss b/app/assets/stylesheets/config/_bootstrap_variables.scss index b6625ee..cffb73a 100755 --- a/app/assets/stylesheets/config/_bootstrap_variables.scss +++ b/app/assets/stylesheets/config/_bootstrap_variables.scss @@ -21,7 +21,7 @@ $light: $checkout; // Buttons & inputs' radius $border-radius: 2px; -$border-radius-lg: 2px; +$border-radius-lg: 5px; $border-radius-sm: 2px; // Override other variables below! diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4f0e273..24a468e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -76,20 +76,31 @@ def load_cart_prints @prints_cart = session[:prints_cart] else # Return array of prints in cart - @prints_cart = session[:prints_cart].map do |id| - @products.select { |product| product['id'] == id } - end + # @prints_cart = session[:prints_cart].map do |id| + # @products.select { |product| product['id'] == id } + # end + + # @flat_cart_arr = @prints_cart.flatten + + @prints_cart = session[:prints_cart].map do |item| + product = @products.find { |product| product['id'] == item["id"] } + if product + product.merge("variant_id" => item["variant_id"]) + else + nil + end + end.compact - @flat_cart_arr = @prints_cart.flatten + @flat_cart_arr = @prints_cart @prints_total = @flat_cart_arr.reduce(0) do |sum, product| sum + product["price"] end puts "==========================================" - puts "Prints cart is:" - puts @prints_cart.flatten.inspect - puts "total is: " + puts "Prints cart is: \n" + puts @prints_cart.inspect + puts "\n total is: " puts @prints_total puts "==========================================" end diff --git a/app/controllers/custom_devise/sessions_controller.rb b/app/controllers/custom_devise/sessions_controller.rb index 06ebea3..0034553 100644 --- a/app/controllers/custom_devise/sessions_controller.rb +++ b/app/controllers/custom_devise/sessions_controller.rb @@ -99,11 +99,23 @@ def load_cart_prints @prints_cart = session[:prints_cart] else # Return array of prints in cart - @prints_cart = session[:prints_cart].map do |id| - @products.select { |product| product['id'] == id } - end + # @prints_cart = session[:prints_cart].map do |id| + # @products.select { |product| product['id'] == id } + # end + + # @flat_cart_arr = @prints_cart.flatten + + @prints_cart = session[:prints_cart].map do |item| + product = @products.find { |product| product['id'] == item["id"] } + if product + product.merge("variant_id" => item["variant_id"]) + else + nil + end + end.compact + + @flat_cart_arr = @prints_cart - @flat_cart_arr = @prints_cart.flatten @prints_total = @flat_cart_arr.reduce(0) do |sum, product| sum + product["price"] diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index b759309..8df34d0 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -424,7 +424,7 @@ def submit_printify_order else all_items << { "product_id": print["id"], # string - "variant_id": print["variant"], # integer + "variant_id": print["variant_id"].present? ? print["variant_id"] : print["variant"], # integer "quantity": 1 } end diff --git a/app/controllers/prints_controller.rb b/app/controllers/prints_controller.rb index 7843cd0..e5a30cd 100644 --- a/app/controllers/prints_controller.rb +++ b/app/controllers/prints_controller.rb @@ -3,6 +3,7 @@ require 'uri' require 'net/http' require 'nokogiri' +require 'byebug' class PrintsController < ApplicationController skip_before_action :authenticate_user!, only: [ :index , :show, :add_to_cart_prints, :remove_from_cart_prints ] @@ -63,17 +64,11 @@ def show variants = print['variants'] - puts"==========================================" - puts"Default variant is: #{default_variant}" - puts"==========================================" - if default_variant price = default_variant['price'] - variant = default_variant['id'] else first_variant = variants.first price = first_variant['price'] - variant = first_variant['id'] end if print["images"].empty? @@ -84,7 +79,8 @@ def show 'description' => parsed_description.text, 'image' => 'abstractart.png', 'price' => price, - 'variants' => variants || [] + 'tags' => print['tags'], + 'variants' => variants || [default_variant] } else @print = { @@ -93,7 +89,8 @@ def show 'description' => parsed_description.text, 'images' => images, 'price' => price, - 'variants' => variants || [] + 'tags' => print['tags'], + 'variants' => variants || [default_variant] } end @@ -103,14 +100,19 @@ def show def add_to_cart_prints id = params[:id] - session[:prints_cart] << id + variant_id = params[:variant_id] + session[:prints_cart] << {"id" => id, "variant_id" => variant_id } redirect_to new_order_path end def remove_from_cart_prints id = params[:id] print_cart = session[:prints_cart] - print_cart.delete_at(print_cart.index(id) || print_cart.length) + index = print_cart.index { |item| item["id"] == id } # && item["variant_id"] == params[:variant_id] TO DO + print_cart.delete_at(index) if index + + # print_cart.delete_at((print_cart.find { |item| item["id"] == id }) || print_cart.length) + # print_cart.delete_at(print_cart.index(id) || print_cart.length) # session[:prints_cart].delete(id) if print_cart.empty? && session[:cart].empty? redirect_to prints_path @@ -251,7 +253,8 @@ def load_products 'title' => product['title'], 'description' => parsed_description.text, 'image' => 'abstractart.png', - 'price' => price, # product['variants'].first['price'], + 'price' => price, + 'tags' => product['tags'], # product['variants'].first['price'], 'variant' => variant # product['variants'].first['id'] } else @@ -261,6 +264,7 @@ def load_products 'description' => parsed_description.text, 'image' => product["images"].first["src"], 'price' => price, + 'tags' => product['tags'], 'variant' => variant } end diff --git a/app/views/prints/show.html.erb b/app/views/prints/show.html.erb index dc22353..a6bebd7 100644 --- a/app/views/prints/show.html.erb +++ b/app/views/prints/show.html.erb @@ -21,11 +21,11 @@ <% end %> - - @@ -41,22 +41,39 @@

<%= @print["description"] %>

- <% puts "PRINT: #{@print}"%> - <% if !@print["variants"].empty? %> -

Available Sizes:

- <%= select_tag "Sizes", options_for_select(@print['variants'].collect{ |v| [v['title'], v['id'].to_i] })%> - <% end %> +
+ + <%= form_with url: add_to_cart_prints_path(@print["id"]), method: :post, local: true do |f| %> + + <% if !(@print["variants"].count < 2) && !@print['tags'].include?("Canvas" || "Framed") %> +

Available Options:

+ <%= f.select :variant_id, @print['variants'].collect{ |v| [v['title'], v['id'].to_i] }, { selected: @print['variants'].first['id'] }, { class: "form-select border border-2 border-radius-lg" } %> + <% else %> + <%= f.hidden_field :variant_id, value: @print['variants'].first['id'] %> + <% end %> + + + <% if @flat_cart_arr.any? { |print| print["id"] == @print["id"]} %> +
+ <%= submit_tag "Add Another", class: "btn btn-lg btn-primary mb-2" %> +
+ <% else %> +
+ <%= submit_tag "Purchase Item", class: "btn btn-lg btn-outline-primary tree mt-3 mb-0" %> +
+ <% end %> + <% end %> + <% if @flat_cart_arr.any? { |print| print["id"] == @print["id"]} %> +
+ <%= button_to 'Remove from cart', remove_from_cart_prints_path(@print["id"]), method: :delete, class: 'btn btn-lg btn-danger mb-2' %> +
+ <% end %> + - <% if @flat_cart_arr.any? { |print| print["id"] == @print["id"]} %> -
- <%= button_to 'Remove from cart', remove_from_cart_prints_path(@print["id"]), method: :delete, class: 'btn btn-lg btn-danger' %> - <%= button_to "Add Another", add_to_cart_prints_path(@print["id"]), method: :post, class: "btn btn-lg btn-primary" %>
- <% else %> -

- <%= button_to "Purchase Item", add_to_cart_prints_path(@print["id"]), method: :post, class: "btn btn-lg btn-outline-primary tree mt-3 mb-0" %> -

- <% end %> + + +