[英]How to add an extra shipping cost to the total price of my shopping cart in ruby on rails?
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.
我希望用户 select 国家运输选项的形式检查我有以下1 ,将 +150 添加到购物车的总价格,当他们 select 个人交付仅 CDMX 仅显示产品的总价格。 I had thought and even tried with an if but did not know how to implement it.
我曾想过甚至尝试过 if 但不知道如何实现它。
This is my shopping_cart.rb class from app / models这是我来自 app / models 的 shopping_cart.rb class
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这是我的 show.haml class 来自 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这是我来自应用程序/控制器的 shopping_carts_controller.rb class
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.您需要告诉 controller 选择了任一选项。
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.执行此操作的基本方法是用户在 select 无线电提交到 controller 方法后按下一个按钮。 This controller method will tell your shopping cart if it becomes either a National Delivery or a Personal Pickup type.
此 controller 方法将告诉您的购物车是国家配送还是个人取货类型。
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.下一个级别是将表单提交转换为 AJAX。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.