Skip to content

Commit

Permalink
product items
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Aug 12, 2024
1 parent b52e8c0 commit 177c0c9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
21 changes: 12 additions & 9 deletions app/controllers/product_checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ def success
stripe_session = Stripe::Checkout::Session.retrieve(@purchase.stripe_session_id)

if stripe_session.payment_status == 'paid'
shipping_cost = stripe_session.shipping_cost.amount_total / 100.0
shipping_cost = stripe_session.shipping_cost.amount_total / 100.0 rescue 0
total_amount = stripe_session.amount_total / 100.0

payment_intent = Stripe::PaymentIntent.retrieve(stripe_session.payment_intent)

@purchase.update(
status: :completed,
shipping_address: stripe_session.shipping_details.address.to_h,
shipping_name: stripe_session.shipping_details.name,
phone: stripe_session.customer_details.phone,
shipping_address: stripe_session&.shipping_details&.address&.to_h,
shipping_name: stripe_session&.shipping_details&.name,
phone: stripe_session&.customer_details&.phone,
shipping_cost: shipping_cost,
total_amount: total_amount,
payment_intent_id: payment_intent["id"]
Expand All @@ -94,11 +94,14 @@ def success

@purchase.product_purchase_items.create(cart.product_cart_items.map { |item|
product = item.product
shipping = product.product_shippings.find_by(country: stripe_session.shipping_details.address.country) ||
product.product_shippings.find_by(country: 'Rest of World')

additional_shipping_cost = shipping ? (item.quantity - 1) * shipping.additional_cost : 0

shipping = nil
additional_shipping_cost = 0
if stripe_session.shipping_details.present?
shipping = product.product_shippings.find_by(country: stripe_session.shipping_details.address.country) ||
product.product_shippings.find_by(country: 'Rest of World')

additional_shipping_cost = shipping ? (item.quantity - 1) * shipping.additional_cost : 0
end
{
product: product,
quantity: item.quantity,
Expand Down
4 changes: 4 additions & 0 deletions app/models/product_purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ def can_refund?
completed? || shipped? || delivered?
end

def total_quantity
product_purchase_items.sum(&:quantity)
end

end
5 changes: 4 additions & 1 deletion app/views/product_purchases/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<p class="mt-2 text-sm font-medium text-default">
Shipping cost: <%= number_to_currency(item.shipping_cost) %>
</p>
<p class="mt-2 text-sm font-medium text-default">
Quantity: <%= item.quantity %>
</p>
</div>

<% end %>
Expand All @@ -67,7 +70,7 @@

<span class="block"><%= purchase.line1 %></span>
<span class="block"><%= purchase.line2 %> <%= purchase.postal_code %></span>
<span class="block"><%= purchase.city %>, <%= purchase.state %>, <%= purchase.country %></span>
<span class="block"><%= [purchase.city, purchase.state, purchase.country].compact.to_sentence %></span>
</dd>
</div>

Expand Down
4 changes: 3 additions & 1 deletion app/views/sales/product_show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<dl class="sm:divide-y sm:divide-emphasis">
<div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt class="text-sm font-medium text-muted">Quantity</dt>
<dd class="mt-1 text-sm text-default sm:mt-0 sm:col-span-2"><%= @product_item.product_purchase_items.size %></dd>
<dd class="mt-1 text-sm text-default sm:mt-0 sm:col-span-2">
<%= @product_item.total_quantity %>
</dd>
</div>
<div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt class="text-sm font-medium text-muted">Price</dt>
Expand Down
22 changes: 17 additions & 5 deletions app/views/shared/_user_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@
class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.my_products_merch") %>
<% end %>
<% end %>
<%= link_to "/sales",
class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.my_sales") %>
<%= link_to "/sales",
class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.my_sales") %>
<% end %>
<% end %>
<%= link_to user_settings_path(current_user.username),
Expand Down Expand Up @@ -387,6 +387,18 @@
<%= link_to "/purchases/tickets", class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.my_purchases") %>
<% end %>
<% if current_user&.can_sell_products? %>
<%= link_to user_products_path(current_user.username),
class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.my_products_merch") %>
<% end %>
<%= link_to "/sales",
class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.my_sales") %>
<% end %>
<% end %>
<%= link_to user_settings_path(current_user.username), class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %>
<%= t("menu.settings") %>
Expand Down

0 comments on commit 177c0c9

Please sign in to comment.