How to call a custom class method when the Spree order ends! executed method

I am developing a spree extension. I want to do to create a new database record at the end of the order. I need some suggestion on how to do this. As far as I understand, one way is to create a custom method in the class of the class and register a new hook (where should I register a new hook? In the initializer?). Another way is to use activators, but I don't know how to subscribe to events. And where should I put code that subscribes to order events.

module Spree
  class Credit < ActiveRecord::Base
     def create_new_line(order) 
        #I need call this method when order finalized    
     end  
  end
end 

I have found a solution. My order decorator looks like this.

Spree::Order.class_eval do
  register_update_hook :add_user_credits
  def add_user_credits
    if (!self.user.nil? and !self.completed_at.nil?)
      # do some stuff, only for registered users and when order complete
    end
  end
end
+4
source share
1 answer

, hook , oder. , - , . , , , Spree . :

Spree::Order.class_eval do
  state_machine do
    after_transition :to => :complete, :do => :add_user_credits
  end

  def add_user_credits        
    # do some stuff
  end
end

, , .

+9

Source: https://habr.com/ru/post/1526214/


All Articles