Add to cart in Rails
ADD TO CART
Step1.Add the link of add to cart
<a href="/products/<%= product.id %>/cart"><i class="ion-android-cart"></i>
</a>
Step 2.create a method in product controller-->
def add_to_cart
@product = Product.find(params[:product_id])
if @product
if user_signed_in?
if current_cart.present?
cart_item = current_cart.cart_items.find_by_product_id(@product.id)
if cart_item.blank?
cart_item = current_cart.cart_items.new(product_id: @product.id)
end
cart_item.unit_price = @product.price
# cart_item.price = cart_item.unit_price * cart_item.quantity
cart_item.price = cart_item.unit_price * 1
if (cart_item.save)
flash[:notice] = "Product has been added into your cart"
redirect_to root_path
end
else
end
else
flash[:notice] = "you need to sign in or sign up"
redirect_to "/users/sign_in"
end
end
end
step 3.create a method in carts controller
def add_to_cart
@current_user.cart.cart_items(params[:product_id , :cart_id])
redirect_to carts_path(current_cart.id)
end
step 3.
create a method in cart_item controller
def update_cart_item_quantity
cart_item = CartItem.find(params[:cart_item_id])
if params[:type] == "increase"
cart_item.update(quantity: cart_item.quantity + 1)
cart_item.price = cart_item.quantity * cart_item.unit_price
cart_item.save
elsif params[:type] == "decrease" && cart_item.quantity != 1
cart_item.update(quantity: cart_item.quantity - 1)
cart_item.price = cart_item.quantity * cart_item.unit_price
cart_item.save
end
end
end
Step 4.Set the routes path
post '/add_to_cart/:product_id' => 'carts#add_to_cart', :as => 'add_to_cart'
Lets go to the Carts index page i.e. index.html.erb
add this view
<p id="notice"><%= notice %></p>
<div class="container">
<br><br> <br>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<% @products.each do |product| %>
<figure class="snip1423">
<%= image_tag(product.image_url, :size => "260x180") %>
<figcaption>
<h3><%= product.name %></h3>
<p><%= product.description%></p>
<div class="price">
<s> ₹<%= product.full_price%></s>
₹<%=product.price %>
</div>
</figcaption><a href="/products/<%= product.id %>/cart"><i class="ion-android-cart"></i>
</a>
<%= link_to 'Edit', edit_product_path(product) %>
</figure>
<% end %>
</div>
<style>
lets enjoy!
Step1.Add the link of add to cart
<a href="/products/<%= product.id %>/cart"><i class="ion-android-cart"></i>
</a>
Step 2.create a method in product controller-->
def add_to_cart
@product = Product.find(params[:product_id])
if @product
if user_signed_in?
if current_cart.present?
cart_item = current_cart.cart_items.find_by_product_id(@product.id)
if cart_item.blank?
cart_item = current_cart.cart_items.new(product_id: @product.id)
end
cart_item.unit_price = @product.price
# cart_item.price = cart_item.unit_price * cart_item.quantity
cart_item.price = cart_item.unit_price * 1
if (cart_item.save)
flash[:notice] = "Product has been added into your cart"
redirect_to root_path
end
else
end
else
flash[:notice] = "you need to sign in or sign up"
redirect_to "/users/sign_in"
end
end
end
step 3.create a method in carts controller
def add_to_cart
@current_user.cart.cart_items(params[:product_id , :cart_id])
redirect_to carts_path(current_cart.id)
end
step 3.
create a method in cart_item controller
def update_cart_item_quantity
cart_item = CartItem.find(params[:cart_item_id])
if params[:type] == "increase"
cart_item.update(quantity: cart_item.quantity + 1)
cart_item.price = cart_item.quantity * cart_item.unit_price
cart_item.save
elsif params[:type] == "decrease" && cart_item.quantity != 1
cart_item.update(quantity: cart_item.quantity - 1)
cart_item.price = cart_item.quantity * cart_item.unit_price
cart_item.save
end
end
end
Step 4.Set the routes path
post '/add_to_cart/:product_id' => 'carts#add_to_cart', :as => 'add_to_cart'
Lets go to the Carts index page i.e. index.html.erb
add this view
<p id="notice"><%= notice %></p>
<div class="container">
<br><br> <br>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<% @products.each do |product| %>
<figure class="snip1423">
<%= image_tag(product.image_url, :size => "260x180") %>
<figcaption>
<h3><%= product.name %></h3>
<p><%= product.description%></p>
<div class="price">
<s> ₹<%= product.full_price%></s>
₹<%=product.price %>
</div>
</figcaption><a href="/products/<%= product.id %>/cart"><i class="ion-android-cart"></i>
</a>
<%= link_to 'Edit', edit_product_path(product) %>
</figure>
<% end %>
</div>
<style>
lets enjoy!
Comments
Post a Comment