Skip to content

Commit

Permalink
Working add and delete methods variant id.
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelLozan committed Dec 22, 2024
1 parent 9661eac commit 3a52ff5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/config/_bootstrap_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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!
25 changes: 18 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions app/controllers/custom_devise/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions app/controllers/prints_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down Expand Up @@ -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?
Expand All @@ -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 = {
Expand All @@ -93,7 +89,8 @@ def show
'description' => parsed_description.text,
'images' => images,
'price' => price,
'variants' => variants || []
'tags' => print['tags'],
'variants' => variants || [default_variant]
}
end

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -261,6 +264,7 @@ def load_products
'description' => parsed_description.text,
'image' => product["images"].first["src"],
'price' => price,
'tags' => product['tags'],
'variant' => variant
}
end
Expand Down
49 changes: 33 additions & 16 deletions app/views/prints/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
</div>
<% end %>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#productCarouselControls" data-bs-slide="prev">
<button class="carousel-control-prev" style="filter: invert(80%);" type="button" data-bs-target="#productCarouselControls" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#productCarouselControls" data-bs-slide="next">
<button class="carousel-control-next" style="filter: invert(80%);" type="button" data-bs-target="#productCarouselControls" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
Expand All @@ -41,22 +41,39 @@

<p class="px-5"><%= @print["description"] %></p>

<% puts "PRINT: #{@print}"%>
<% if !@print["variants"].empty? %>
<p>Available Sizes:</p>
<%= select_tag "Sizes", options_for_select(@print['variants'].collect{ |v| [v['title'], v['id'].to_i] })%>
<% end %>
<div class="d-flex flex-column gap-1 my-1 px-5 text-center col-md-6 mx-auto">

<%= 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") %>
<p>Available Options:</p>
<%= 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"]} %>
<div class="d-flex flex-column mt-3">
<%= submit_tag "Add Another", class: "btn btn-lg btn-primary mb-2" %>
</div>
<% else %>
<div class="d-flex flex-column mt-3">
<%= submit_tag "Purchase Item", class: "btn btn-lg btn-outline-primary tree mt-3 mb-0" %>
</div>
<% end %>
<% end %>
<% if @flat_cart_arr.any? { |print| print["id"] == @print["id"]} %>
<div class="d-flex flex-column mt-0">
<%= button_to 'Remove from cart', remove_from_cart_prints_path(@print["id"]), method: :delete, class: 'btn btn-lg btn-danger mb-2' %>
</div>
<% end %>


<% if @flat_cart_arr.any? { |print| print["id"] == @print["id"]} %>
<div class="d-flex flex-column">
<%= 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" %>
</div>
<% else %>
<p>
<%= 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" %>
</p>
<% end %>






Expand Down

0 comments on commit 3a52ff5

Please sign in to comment.