簡體   English   中英

如何在 ruby on rails 的購物車總價中增加額外的運費?

[英]How to add an extra shipping cost to the total price of my shopping cart in ruby ​on rails?

顯然我頁面上的所有內容都“很好”,但我想進行以下修改,希望你能幫助我。 我希望用戶 select 國家運輸選項的形式檢查我有以下1 ,將 +150 添加到購物車的總價格,當他們 select 個人交付僅 CDMX 僅顯示產品的總價格。 我曾想過甚至嘗試過 if 但不知道如何實現它。

這是我來自 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

這是我的 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

這是我來自應用程序/控制器的 shopping_carts_controller.rb class

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

您需要告訴 controller 選擇了任一選項。

執行此操作的基本方法是用戶在 select 無線電提交到 controller 方法后按下一個按鈕。 此 controller 方法將告訴您的購物車是國家配送還是個人取貨類型。

大致是這樣的:

# 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM