2

我的环境。

Mac OSX 10.7.4
Ruby 1.9.3p0
Rails 3.2.3
Spree 1.1.0

你好,我是日本程序员和 Spree 的新手。我想自定义 Spree 的结帐流程以删除结帐步骤,例如选择送货方式和付款方式。

根据Spree Guides,我需要为模型定义一个全新的状态机Order

所以,我添加order_decorator.rbapp/models目录,如下所示。

# app/models/order_decorator.rb

module SpreeCustomExtension
  class Engine < Rails::Engine
    def self.activate
      Order.class_eval do
        Order.state_machines[:state] = StateMachine::Machine.new(Order, :initial => 'cart') do
          after_transition :to => 'complete', :do => :complete_order
          before_transition :to => 'complete', :do => :process_payment

          event :next do
            transition :from => 'cart', :to => 'payment'
            transition :from => 'payment', :to => 'confirm'
            transition :from => 'confirm', :to => 'complete'
          end

          event :cancel do
            transition :to => 'canceled', :if => :allow_cancel?
          end
          event :return do
            transition :to => 'returned', :from => 'awaiting_return'
          end
          event :resume do
            transition :to => 'resumed', :from => 'canceled', :if => :allow_resume?
          end
          event :authorize_return do
            transition :to => 'awaiting_return'
          end

          before_transition :to => 'complete' do |order|
            begin
              order.process_payments!
            rescue Spree::GatewayError
              if Spree::Config[:allow_checkout_on_gateway_error]
                true
              else
                false
              end
            end
          end

          after_transition :to => 'complete', :do => :finalize!
          after_transition :to => 'confirm', :do => :create_tax_charge!
          after_transition :to => 'payment', :do => :create_shipment!
          after_transition :to => 'canceled', :do => :after_cancel

        end
      end
    end
  end
end

另外,我将spree_site.rb文件添加到我的lib/目录中。

# lib/spree_site.rb

module SpreeSite
  class Engine < Rails::Engine
    def self.activate
      Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
        Rails.configuration.cache_classes ? require(c) : load(c)
      end
    end

    def load_tasks
    end

    config.to_prepare &method(:activate).to_proc
  end
end

但是,似乎完全没有变化。有人知道我应该在哪里修改以自定义结帐流程吗?

请提出一些好的狂欢教程或建议。我真的很感激任何建议。

先感谢您。

4

2 回答 2

4

您需要在 Spree 命名空间内,覆盖 Spree::Order 而不仅仅是 Order。

于 2012-06-05T00:48:48.313 回答
3

我认为你的订单装饰器看起来应该有点不同。只做一个文件app/models/spree/order_decorator.rb

然后像这样开始

Spree::Order.class_eval do
  state_machine do
    before_transition to: "address" do |order|
    ...
  end
end
于 2012-07-12T13:50:14.817 回答