Hi,
I need to run some code once the order is fully paid for. I have tried
using after transition to ready in Shipment:
Spree::Shipment.class_eval do
   self.state_machine.after_transition :to => :ready, :do => :
do_something_important

   def do_something_important
     Rails.logger.info "Being very important"
   end
end

I also tried something similar in Payment:

Spree::Payment.class_eval do
   self.state_machine.after_transition :to => 'complete', :do =>
:do_something_important

   def do_something_important
     Rails.logger.info "Being very important"
   end
end

I specify a cheque payment in the order, and then using the admin backend,
I mark the cheque as having being lodged, and paid for.

In my logs, I am seeing the following inserts into the spree_state_changes
table:

INSERT INTO "spree_state_changes" ("created_at", "name", "next_state",
"previous_state", "stateful_id", "stateful_type", "updated_at", "user_id")
VALUES ('2013-10-07 14:44:35.958000', 'shipment', 'ready', 'pending', 6,
'Spree::Order', '2013-10-07 14:44:35.958000', 1) RETURNING "id"

Is there something glaringly obvious that I'm not doing?

Search Discussions

  • Yudi at Sep 4, 2015 at 12:23 am
    Revisiting this post. Even though what you did here fixes the callback on
    payment completion, it seems to me that the code not getting triggered on
    shipment ready is a separate problem.

    The shipment status is updated by this update method
    (https://github.com/spree/spree/blob/2-4-stable/core/app/models/spree/shipment.rb#L339-L350)
    which, according to the comment, bypasses all the callbacks.

    I wonder if there is a workaround if I want to trigger some code on
    shipment ready (say, automatically notify my fulfillment center to ship the
    item). Or maybe, is there a better event for me to attach the callback?

    Thanks,
    Yudi

    On Tuesday, October 8, 2013 at 2:52:27 AM UTC-7, Jonathan O'Connor wrote:

    Found the cursed error! The transition in the Payment state machine must
    be on reaching the 'completed' state, not 'complete'.
    Corrected line in Payment decorator:
    self.state_machine.after_transition :to => 'completed', :do =>
    :do_something_special



    It would be nice if the state machine generated an error when calling
    after_transition with an unknown state. Maybe this can't be done.

    Slowly starting to grok Spree!
  • Jonathan O'Connor at Sep 4, 2015 at 7:16 pm
    Yudi,
    I am sorry to say that the project I was working on is now dead. Good luck
    in finding a solution.

    Jonathan
    On Fri, 4 Sep 2015 01:23 wrote:

    Revisiting this post. Even though what you did here fixes the callback on
    payment completion, it seems to me that the code not getting triggered on
    shipment ready is a separate problem.

    The shipment status is updated by this update method (
    https://github.com/spree/spree/blob/2-4-stable/core/app/models/spree/shipment.rb#L339-L350)
    which, according to the comment, bypasses all the callbacks.

    I wonder if there is a workaround if I want to trigger some code on
    shipment ready (say, automatically notify my fulfillment center to ship the
    item). Or maybe, is there a better event for me to attach the callback?

    Thanks,
    Yudi

    On Tuesday, October 8, 2013 at 2:52:27 AM UTC-7, Jonathan O'Connor wrote:

    Found the cursed error! The transition in the Payment state machine must
    be on reaching the 'completed' state, not 'complete'.
    Corrected line in Payment decorator:
    self.state_machine.after_transition :to => 'completed', :do =>
    :do_something_special



    It would be nice if the state machine generated an error when calling
    after_transition with an unknown state. Maybe this can't be done.

    Slowly starting to grok Spree!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupspree-user @
categoriesrubyonrails
postedOct 7, '13 at 3:21p
activeSep 4, '15 at 7:16p
posts3
users2
websitespreecommerce.com
irc#RubyOnRails

2 users in discussion

Jonathan O'Connor: 2 posts Yudi: 1 post

People

Translate

site design / logo © 2022 Grokbase