Skip to content

Commit

Permalink
レビュー対応在庫数を減らす処理を復活
Browse files Browse the repository at this point in the history
  • Loading branch information
KOH6 committed Sep 12, 2023
1 parent 42c9498 commit d60f67d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def create
end

if @order.save
# Orderレコード作成時に、カートとのリレーションの作成、商品の在庫数更新を行う
decrease_product_stock(cart_products: @cart_products)
session[:cart_id] = nil
flash[:success] = '購入ありがとうございます'
OrderMailer.complete(order: @order).deliver_later
Expand All @@ -43,6 +45,14 @@ def set_cart_products
@total = @cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal }
end

def decrease_product_stock(cart_products:)
cart_products.each do |cart_product|
product = Product.find(cart_product.product_id)
quantity = cart_product.quantity
Product.update(cart_product.product_id, stock: product.stock - quantity)
end
end

def out_of_stock?(cart_products:)
cart_products.each do |cart_product|
product = Product.find(cart_product.product_id)
Expand Down

0 comments on commit d60f67d

Please sign in to comment.