【问题标题】:How to add an extra shipping cost to the total price of my shopping cart in ruby ​on rails?如何在 ruby​​ on rails 购物车的总价中增加额外的运费?
【发布时间】:2021-08-27 03:52:40
【问题描述】:

显然我页面上的所有内容都“很好”,但我想进行以下修改,希望您能帮助我????。 我希望用户选择我在1 下方的表格检查的国家运输选项,将 +150 添加到购物车的总价格,并且当他们选择仅个人交付 CDMX 仅显示产品的总价。我曾想过甚至尝试过 if 但不知道如何实现它。

这是我在 app/models 中的 shopping_cart.rb 类

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

这是我来自 app/views/shopping_carts 的 show.haml 类

%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

这是我的应用/控制器中的 shopping_carts_controller.rb 类

class ShoppingCartsController < ApplicationController
    def show
        if @shopping_cart.payed?
            render "shopping_carts/complete"
            return
        end
    end
end
            

【问题讨论】:

    标签: ruby-on-rails e-commerce shopping-cart shipping-method


    【解决方案1】:

    您需要告诉控制器已选择任一选项。

    执行此操作的基本方法是用户在选择收音机后按下按钮以提交给控制器方法。此控制器方法将告诉您的购物车是国家送货还是个人取货类型。

    大概是这样的:

    # 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
    

    从那里,您可以相应地修改总数。

    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
    

    下一个级别是将表单提交转换为 AJAX。

    【讨论】:

    • 我已经编辑了我的评论,你能告诉我即使我更新了你的答案是否有效吗?
    • 嗨@VictordeGallegos,上面的视图和控制器代码不能保证按所写的那样工作。更多的是向您展示有关它的粗略想法。您必须编写路由和所有其他额外的实现细节。
    猜你喜欢
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多