Apparently everything on my page is "fine" but I would like to make the following modifications, I hope you can help me. I would like the user to select the National shipping option of the form check that I have below 1 , add +150 to the Total price of the cart and when they select Personal delivery only CDMX only the total price of the products is displayed. I had thought and even tried with an if but did not know how to implement it.
This is my shopping_cart.rb class from app / models
class ShoppingCart < ApplicationRecord
has_many :in_shopping_carts
has_many :products, through: :in_shopping_carts
enum status: {payed: 1, default: 0}
def total
products.sum(:pricing)
end
end
This is my show.haml class from app / views / shopping_carts
%h1.mb-3.text-align:center.text-center Mi carrito
.form-check
%p Selecciona tu tipo de envio:
%input#exampleRadios1.form-check-input{checked: "checked", name: "exampleRadios", type: "radio", value: "option1"}/
%label.form-check-label{for: "exampleRadios1"}
Entrega personal solo CDMX
.form-check
%input#exampleRadios2.form-check-input{name: "exampleRadios", type: "radio", value: "option2"}/
%label.form-check-label{for: "exampleRadios2"}
Envio nacional
.card.large-padding
%table.table.table-striped.table-hover.small#table_shopping_cart
%thead
%td Producto
%td Costo
%td Acciones
%tbody
-@shopping_cart.in_shopping_carts.each do |i_sh|
-product = i_sh.product
%tr{id: "#{product.id}"}
%td= product.name
%td= product.pricing
%td= link_to "Eliminar",i_sh,method: :delete
.top-space.large-padding.text-center#payment_form
%p.medium
%strong
Total: $
=@shopping_cart.total
And This is my shopping_carts_controller.rb class from app / controllers
class ShoppingCartsController < ApplicationController
def show
if @shopping_cart.payed?
render "shopping_carts/complete"
return
end
end
end
You will need to tell the controller that either option was selected.
The basic way to do this is for the user to press a button after they select the radio to submit to a controller method. This controller method will tell your shopping cart if it becomes either a National Delivery or a Personal Pickup type.
Roughly something like:
# view
form_tag change_delivery_method_path(@shopping_cart)
label
Personal
radio_button_tag "delivery_method", "personal"
label
National
radio_button_tag "delivery_method", "national"
submit_button_tag
# controller
def change_delivery_method
shopping_cart = ShoppingCart.find(params[:shopping_cart_id])
shopping_cart.update(delivery_method: params[:delivery_method])
end
From there, you can modify the total accordingly.
class ShoppingCart < ApplicationRecord
has_many :in_shopping_carts
has_many :products, through: :in_shopping_carts
enum status: {payed: 1, default: 0}
enum delivery_method: { personal: 0, national: 1 }
def total
products.sum(:pricing) + delivery_fee
end
def delivery_fee
return 150 if national?
return 0 if personal?
end
end
The next level would be converting the form submit into AJAX.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.