Permalink
Browse files

reduce checkout step, #408.

  • Loading branch information...
saberma committed Feb 22, 2012
1 parent a417eae commit 28160d8a05a46105e1103361336fee947a392f07
@@ -1,8 +1,7 @@
#=require jquery
#=require jquery_ujs
-#地区(冗余代码待重构)
-RegionUtils =
+RegionUtils = #地区(冗余代码待重构)
init: (seed = [], region = '.region') ->
$(region).each ->
selects = $('select', this)
@@ -20,78 +19,74 @@ RegionUtils =
value = seed[select_index]
select.val(value).change() if value # 级联回显
-$(document).ready ->
+Validator = # 校验
+ validate_blank: (id, field)-> # 是否为空
+ obj = $("##{id}")
+ if obj.val() is '' # order_email
+ $("#error_#{id}").text("#{field}不能为空!").show() # error_order_email
+ obj.focus()
+ false
+ else
+ $("#error_#{id}").hide()
+ true
- #处理地址,级联操作税率
- $('#order_shipping_address_attributes_country_code').each ->
- $(this).change ->
- country_code = $(this).val()
- if country_code isnt 'CN'
- $(this).closest('table').find('.region').parent().hide()
- else
- $(this).closest('table').find('.region').parent().show()
- action = $(this).closest('form').attr('action').replace(/create_order/i,'update_tax_price')
- $.post action, { country_code: country_code, shipping_same: checked }, (data) ->
- img = $("#cost :first-child")[0]
- $('#cost').html('¥' + data.total_price).append(img)
- $('#tax_span').html(" ..包含#{data.order_tax_price}元的税")
- $(this).ajaxStart ->
- $('.spinner').show()
- $(this).ajaxStop ->
- $('.spinner').hide()
+ valid_email: (id, field)->
+ filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9_]{2,4})+$/
+ obj = $("##{id}")
+ if !filter.test(obj.val())
+ $("#error_#{id}").text("#{field}格式不正确!").show() # error_order_email
+ obj.focus()
+ false
+ else
+ $("#error_#{id}").hide()
+ true
+
+ validates: (fields)-> # 校验
+ flag = true
+ $.each fields, (id, field) ->
+ flag = Validator.validate_blank(id, field)
+ return flag
+ flag
- #/carts/xxx 页面处理货品地址和发单地址
- #$('#shipping-toggle').change ->
- # checked = $(this).attr('checked') is 'checked'
- # $('#shipping').toggle !checked
- # $('#shipping-same').toggle checked
- # if checked
- # $('#order_billing_address_attributes_country_code').change()
- # else
- # $('#order_shipping_address_attributes_country_code').change()
- #.change()
+$(document).ready ->
if $('#no-shipping-rates').size() > 0
- $('input#complete-purchase').attr 'disabled', true
+ $('#complete-purchase').attr 'disabled', true
- #处理快递费用
- $('#shipping-rates').change ->
- action = $(this).closest('form').attr('action')
- href = action.substr(0,action.lastIndexOf('/')) + '/update_total_price'
- rate = $(this).val()
- $.post href, { shipping_rate: rate }, (data) ->
- if data.error is 'shipping_rate'
- $('#shipping-rate-error').show()
- $("#shipping-rates option[value='#{data.shipping_rate}']").remove()
- else
- $('#shipping-rate-error').hide()
- img = $("#cost :first-child")[0]
- $('#cost').html('¥' + data.total_price).append(img)
- $('#shipping_span').html(" ..包含快递费#{data.shipping_rate_price}")
- $(this).ajaxStart -> $('.spinner').show()
- $(this).ajaxStop -> $('.spinner').hide()
- .change()
+ $("input[name='order[shipping_rate]']").change -> # 快递费用
+ format = $('#cost').attr('format')
+ price = parseFloat $(this).attr('start')
+ total_price = parseFloat($('#cost').attr('total_price')) + price
+ format_price = format.replace /{{amount}}/, total_price
+ $('#cost').html(format_price)
+ $('#shipping_span').html(" ..包含快递费#{price}")
- #处理订单提交结账
- $("input#complete-purchase").click ->
- $(this).attr('disabled', 'true').val '正在完成订单...'
+ $("#complete-purchase").click -> #处理订单提交结账
+ return false unless Validator.validates {
+ order_email: 'Email地址',
+ order_shipping_address_attributes_name: '姓名',
+ order_shipping_address_attributes_province: '省份',
+ order_shipping_address_attributes_city: '城市',
+ order_shipping_address_attributes_district: '地区',
+ order_shipping_address_attributes_address1: '地址',
+ order_shipping_address_attributes_phone: '电话'
+ }
+ return false unless Validator.valid_email('order_email', 'Email地址')
+ if $("input[name='order[shipping_rate]']:checked").size() is 0
+ alert '请选择配送方式'
+ return false
+ else if $("input[name='order[payment_id]']:checked").size() is 0
+ alert '请选择支付方式'
+ return false
+ $(this).attr('disabled', 'true').val '正在提交...'
form = $(this).closest('form')
- action = form.attr('action')
- attrs = form.serialize()
- $.post action,attrs, (data) ->
- $("input#complete-purchase").attr('disabled', '').val '购买'
- if data.error is 'shipping_rate'
- $('#shipping-rate-error').show()
- $("#shipping-rates option[value='#{data.shipping_rate}']").remove()
- if data.payment_error is true
- $('#payment-error').show()
- if data.success is true
- window.location = data.url
- $(this).ajaxStart -> $('#purchase-progress').show()
- $(this).ajaxStop -> $('#purchase-progress').hide()
+ $('#purchase-progress').show()
+ $.post form.attr('action'), form.serialize(), (data) ->
+ $("#complete-purchase").attr('disabled', '').val '提交订单'
+ $('#purchase-progress').hide()
+ window.location = data.url if data.success is true
false
-
#地区的级联选择
$(".region").each ->
selects = $('select', this)
@@ -144,4 +139,3 @@ $(document).ready ->
$("##{str}_zip").val('')
$("##{str}_company").val('')
$("##{str}_phone").val('')
-
@@ -260,10 +260,6 @@ li.text-payment {
input {
font-size: 150%; } }
-#shipping-same label {
- display: block;
- font-size: 100%; }
-
.address-notification {
background: #ffefeb;
border: 1px solid #f96;
@@ -305,7 +301,7 @@ li.text-payment {
background: #ccc;
text-align: center; }
-#payment-methods {
+#payment-methods, #shipping_rates {
list-style: none;
margin: 0;
padding: 0;
@@ -2,3 +2,8 @@
*= require forms
*= require checkout
*/
+
+.errorHint {
+ display: none;
+ margin-left: 5px;
+ color: red; }
@@ -13,6 +13,9 @@ body, h1, h2, h3, h4, h5, h6 { /* 中文字体友好 */
font-family: Arial,Tahoma,"hiragino sans gb",Helvetica;
}
+#header.frontpage, #banner { /* 背景颜色由黄绿色改为浅绿色 */
+ background: #6FB536 }
+
ul#nav-primary li a.top-nav { /* fixed: ie6 nav height */
color: #ddd;
text-decoration: none;
@@ -1,10 +1,11 @@
#encoding: utf-8
class Shop::OrderController < Shop::AppController
include Admin::ShopsHelper
- skip_before_filter :password_protected , only: [:notify, :done, :tenpay_notify, :tenpay_done]
- skip_before_filter :must_has_theme , only: [:notify, :done, :tenpay_notify, :tenpay_done]
- skip_before_filter :check_shop_avaliable , only: [:notify, :done, :tenpay_notify, :tenpay_done]
- skip_before_filter :check_shop_access_enabled, only: [:notify, :done, :tenpay_notify, :tenpay_done]
+ PAYMENT_METHODS = [:notify, :done, :tenpay_notify, :tenpay_done]
+ skip_before_filter :password_protected , only: PAYMENT_METHODS
+ skip_before_filter :must_has_theme , only: PAYMENT_METHODS
+ skip_before_filter :check_shop_avaliable , only: PAYMENT_METHODS
+ skip_before_filter :check_shop_access_enabled, only: PAYMENT_METHODS
layout 'shop/checkout'
@@ -26,7 +27,7 @@ class Shop::OrderController < Shop::AppController
expose(:cart) { shop.carts.where(token: params[:cart_token]).first }
- before_filter only: :address do
+ before_filter only: :new do
verify_customer!(cart)
end
@@ -41,6 +42,14 @@ class Shop::OrderController < Shop::AppController
end
end
+ expose(:cart_total_weight) do
+ cart_line_items.map do |item|
+ variant = item.first
+ quantity = item.second
+ quantity * variant.weight
+ end.sum
+ end
+
expose(:cart_total_price) do
cart_line_items.map do |item|
variant = item.first
@@ -60,81 +69,45 @@ class Shop::OrderController < Shop::AppController
order.total_price
end
- expose(:countries){
- shop.countries
- }
-
expose(:country){
- order.shipping_address.country
+ shop.countries.where(code: 'CN').first
}
expose(:shipping_rates) do
- total_weight = order.total_weight / 1000.0 # 订单的total_weight以克为单位
+ total_weight = cart_total_weight / 1000.0 # 订单的total_weight以克为单位
country.weight_based_shipping_rates.where(:weight_low.lte => total_weight,:weight_high.gte => total_weight ).all + country.price_based_shipping_rates.where(:min_order_subtotal.lte => order.total_line_items_price,:max_order_subtotal.gte => order.total_line_items_price ).all
end
- # 订单提交Step1
- def address
- if cart.cart_hash.blank?
+ def new # 显示订单表单
+ if cart_line_items.empty?
render(action: 'error', layout: false) and return
end
order.build_shipping_address if order.shipping_address.nil?
end
- # 提交订单: 填写完收货地址等就可以创建订单了
- def create
- address #初始化shipping_address
- order.build_shipping_address(order.shipping_address.attributes)
- JSON(cart.cart_hash).each_pair do |variant_id, quantity| #保存已购买商品
- begin
- variant = shop.variants.find(variant_id)
- order.line_items.build product_variant: variant, price: variant.price, quantity: quantity
- rescue ActiveRecord::RecordNotFound # 款式已被删除
- end
+ def create # 提交订单
+ if cart_line_items.empty?
+ render json: {error: 'unavailable_product'} and return
end
- #税率
- order.tax_price = shop.taxes_included ? 0.0 : cart_total_price * shop.countries.find_by_code(order.shipping_address.country_code).tax_percentage/100
+ order.build_shipping_address(order.shipping_address.attributes)
+ cart_line_items.each_pair do |variant, quantity|
+ order.line_items.build product_variant: variant, price: variant.price, quantity: quantity
+ end
+ data = {}
if order.save
- redirect_to pay_order_path(shop_id: shop.id, token: order.token)
- else
- render action: :address
+ shop.carts.where(token: order.token).first.try(:destroy) # 删除购物车实体
+ order.send_email_when_order_forward if order.payment.name #发送邮件,非在线支付方式。在线支付方式在付款之后发送邮件
+ data = {success: true, url: forward_order_path(params[:shop_id], params[:cart_token])}
end
- end
-
- # 发货方式、付款方式Step2
- def pay
- order.payment ||= shop.payments.first
+ render json: data
end
def forward
render file: 'public/404.html',layout: false, status: 404 unless order
end
- # 支付
- def commit
- data = {}
- include_shipping_rate = shipping_rates.map(&:shipping_rate).include? params[:order][:shipping_rate]
- if !include_shipping_rate || !params[:order][:payment_id]
- data = data.merge({error: 'shipping_rate', shipping_rate: params[:shipping_rate] }) if !include_shipping_rate
- data = data.merge({payment_error: true}) if !params[:order][:payment_id]
- else
- #若是已提交过的订单,则不做任何操作
- if order.payment.nil?
- params[:buyer_accepts_marketing] == 'true' ? order.customer.accepts_marketing = true : order.customer.accepts_marketing = false
- order.customer.save
- order.financial_status = 'pending'
- order.payment = shop.payments.find(params[:order][:payment_id])
- order.save
- shop.carts.where(token: order.token).first.try(:destroy) # 删除购物车实体
- order.send_email_when_order_forward if order.payment.name #发送邮件,非在线支付方式。在线支付方式在付款之后发送邮件
- end
- data = {success: true, url: forward_order_path(params[:shop_id],params[:token])}
- end
- render json: data
- end
-
begin 'from pay gateway'
begin '支付宝'
@@ -188,27 +161,6 @@ def tenpay_done # 支付后从浏览器前台直接返回(show_url)
end
- def update_total_price
- #处理更新快递方式
- if !shipping_rates.map(&:shipping_rate).include? params[:shipping_rate]
- data = {error: 'shipping_rate', shipping_rate: params[:shipping_rate] }
- else
- order.shipping_rate = params[:shipping_rate]
- order.total_price = order.total_line_items_price + params[:shipping_rate].gsub(/.+-/,'').to_f + order.order_tax_price
- order.save
- data = {total_price: order.total_price, shipping_rate_price: order.shipping_rate_price}
- end
- render json: data
- end
-
- def update_tax_price
- country = shop.countries.find_by_code(params[:country_code])
- order_tax_price = shop.taxes_included ? 0.0 : cart_total_price * country.tax_percentage/100
- total_price = cart_total_price + order_tax_price
- data = {total_price: total_price, order_tax_price: order_tax_price}
- render json: data
- end
-
def get_address
customer = cart.customer
address = customer.addresses.where(id: params[:address_id]).first
View
@@ -27,6 +27,7 @@ class Customer < ActiveRecord::Base
attr_accessor :tags_text
default_value_for :status, 'enabled'
+ default_value_for :accepts_marketing, true
def tags_text
@tags_text ||= self.tags.map(&:name).join(', ')
View
@@ -9,15 +9,15 @@ class Order < ActiveRecord::Base
has_many :histories , dependent: :destroy, class_name: 'OrderHistory' , order: :id.desc #订单历史
belongs_to :payment , class_name: 'Payment' #支付方式
- attr_accessible :id, :email, :shipping_rate, :note, :shipping_address_attributes, :cancel_reason, :total_weight
+ attr_accessible :id, :email, :shipping_rate, :note, :shipping_address_attributes, :cancel_reason, :total_weight, :payment_id
accepts_nested_attributes_for :shipping_address
- validates_presence_of :email, message: '此栏不能为空白'
- #validates_presence_of :shipping_rate, :gateway, on: :update
+ validates_presence_of :email, :shipping_address, :shipping_rate, :payment_id, message: '此栏不能为空白'
+ #validates :shipping_rate, inclusion: { in: %w()} # TODO: 配送记录必须存在
default_value_for :status, 'open'
- default_value_for :financial_status, 'abandoned'
+ default_value_for :financial_status, 'pending'
default_value_for :fulfillment_status, 'unshipped'
before_create do
@@ -228,6 +228,8 @@ class OrderShippingAddress < ActiveRecord::Base
belongs_to :order
validates_presence_of :name, :province, :city, :district, :address1, :phone, message: '此栏不能为空白'
+ default_value_for :country_code, 'CN'
+
def country
order.shop.countries.where(code: country_code).first
end
Oops, something went wrong.

0 comments on commit 28160d8

Please sign in to comment.