Search code examples
ruby-on-railsshopping-cartcheckout

How to update stock quantity in shopping cart applicaiton in rails?


I am newbie in rails and building a shopping cart application. It functions as: 1.When the user logs in, he/sher is able to add products to the cart. And the order is saved with that user's id in order table. 2.It consists of products, orders, users and order items table. 3.I am using seeds to display the products list.

I want to display the stocks available for the products and when user enters the quantity and adds the product to the cart. And when user checkouts, the available stock should be decreased.

Can anyone guide me on what methods should be used and where to write them?

EDIT : I have OrderItems controller as follows:

class OrderItemsController < ApplicationController
  before_action :authenticate_user!
  after_action :cart, only: %i[create update destroy]
  def create
    @order = current_order
    @order_item = @order.order_items.new(order_item_params)
    @order.save
    session[:order_id] = @order.id
  end

def remove_stock
  product = @order.order_items.find(params[:id])
  product.stock -= self.quantity
  product.save
  redirect_to cart_path
end
end

And the view for displaying Cart is follow: carts/show.html.erb :

    <h4 class='text-right'>Total Price: <span style='color: green'><%= number_to_currency order_item.total_price %></span></h4>
  <%= link_to 'Buy', remove_stock(order_item)  %>
  <% end %>

One clicking the 'buy' button it should go to the remove_stock method and then the stock should be decreased. However I am not sure what route to use to follow this.

Any suggestions?

Thanks in advance.


Solution

  • I tried it this way and it kinda works. But if you ask me if this is the best practice, I am not sure.

    My Models: 1.User (name, email, password fields, role) 2.Product (name, creator_id, available_quantity, unit, cost_per_unit) 3.Order (product_id, user_id, quantity_required)

    Controllers: 1.users_controller 2.products_controller 3.orders_controller

    1. I had the Products page displaying list of products with available quantity, input to select quantity needed and a 'Add To Cart' button

    2. When the user inputs the quantity needed and clicks on Add To Cart button, I handled the button click to an ajax call sending

      {product_id: '', quantity: '', user_id: ''}

    3. When the Add To Cart ajax is called, it is handled by orders_controller's add_to_cart action which creates a row in orders table with user_id, product_id and status as 'in_progress'. Before Creating the row I also check if the same user has the same product in 'in_progress' state. If so, I consider that he has updated his order. In that case, I update the existing column. One more check that I enabled here is to check the required quantity is less than or equal to the available quantity.

    4. For 'Remove From Cart' I followed the same ajax call process with the differentiation that it removes the column from the orders table.

    5. Finally when the User clicks on 'Check out' button, I get all the orders that are in 'in_progress' status, check the available quantity and required still matches and then send the order to billing. once the billing / payment is successfull, mark the status of the in_progress orders as 'Complete'and here you have to call the update_stock method or simply update the product's available_quantity.