[必備][JDStore] 輕鬆產生商品「優惠折扣」

效果

沒有折扣功能的店太坑爹了!快裝吧

預計時間:5分鐘

  • 給商品在資料庫裡建立一個折扣

$ rails g migration add_discount

db/migrate/XXXXXXX
  def change
    add_column :products, :discount, :integer
  end

rake db:migrate

app/controllers/admin/products_controller.rb
  def product_params
-    params.require(:product).permit(:title, :description, :price, :quantity, :image)  
+    params.require(:product).permit(:title, :description, :price, :quantity, :image, :discount)
  end
  • 優惠幅度須大於0%,小於100%
app/models/product.rb
+ validates_numericality_of :discount, :greater_than => 0, :less_than => 100, :allow_blank => true 
  • 後台管理員可以設定折扣幅度

app/views/admin/products/edit.html.erb
<div class="group">
  <%= f.input :discount,:label => "Discount % (eg. $100 x 80% = $80)" %>
</div>
  • 如有有打折,就把價格改為特價的價格
app/controllers/products_controller.rb
  def discount_price
    @product = Product.find(params[:id])
    if @product.discount.present?()
      @product.price = @product.price * (@product.discount / 100 )
    end
  end
  • 如有有打折,就顯示特價的價格

app/views/products/show.html.erb
-        <div> 数量 : <%= @product.quantity %> </div>
-    <div class="product-price">  <%= @product.price %> </div>
+      <!-- 如果有折扣 -->
+      <% if @product.discount.present? && @product.quantity > 0 %>
+        <!--  顯示折扣% -->
+        <%= @product.discount %>% off
+        <!--  顯示原價,加上刪除線 -->
+        <s><div class="product-price">  <%= @product.price %> </div></s>
+        <!--  顯示優惠價 -->
+        <div class="product-price discount"> <%= @product.price = @product.price * @product.discount/100 %></div>
+      <% else %>
+        <!-- 沒折扣 -->
+        <div class="product-price"> ¥ <%= @product.price %> </div>
+      <% end %>
+        <br>
+        <div> 数量 : <%= @product.quantity %> </div>
  • 修改整體商品頁面價格

app/views/products/index.html.erb
-      <%= product.title %> ¥ <%= product.price %>
+      <%= product.title %>
+      <% if product.discount.present? %>
+        ¥ <%= product.price = product.price * product.discount/100 %>
+      <% else %>
+         <%= product.price %>
+      <% end %>
  • 購物車總價計算
app/models/cart.rb
def total_price
    sum = 0
    cart_items.each do |cart_item|
      if cart_item.product.price.present?
        if cart_item.product.discount.present?
          sum += cart_item.quantity * cart_item.product.price * cart_item.product.discount / 100
        else
          sum += cart_item.quantity * cart_item.product.price
        end
      end
    end
    sum
  end
  • 購物車顯示
app/views/carts/index.html.erb
-        <td><%= cart_item.product.price %></td>
+           <% if cart_item.product.discount.present? %>
+            <td><%= cart_item.product.price * cart_item.product.discount / 100 %></td>
+          <% else %>
+            <td><%= cart_item.product.price %></td>
+          <% end %>
  • 帳單顯示
app/views/carts/checkout.html.erb
-        <td><%= cart_item.product.price %></td>
+           <% if cart_item.product.discount.present? %>
+            <td><%= cart_item.product.price * cart_item.product.discount / 100 %></td>
+          <% else %>
+            <td><%= cart_item.product.price %></td>
+          <% end %>


延伸教學 - [JDStore] 輕鬆產生商品「倒數計時器」

目前Database要怎麼把倒數計時器與折扣價連動在一起還沒做出來,歡迎同學一起討論研究



希望您喜歡這篇文章,請抽空幫我投一票,謝謝!



如果获得2等奖,我会从投票给我的VIP学员里抽出1位赠送这本有香气的励志书


comments powered by Disqus