Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New event "order_status_changed_before_save" #1128

Closed
wants to merge 1 commit into from
Closed

New event "order_status_changed_before_save" #1128

wants to merge 1 commit into from

Conversation

woutersamaey
Copy link
Contributor

@woutersamaey woutersamaey commented Jul 29, 2020

Description (*)

New event "sales_order_state_change" allows custom modules to detect an order state/status change and trigger appropriate actions. I've seen modules rewrite the order model because this was missing.

Very handy improvement.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)

@github-actions github-actions bot added the Component: Sales Relates to Mage_Sales label Jul 29, 2020
@kiatng
Copy link
Collaborator

kiatng commented Jul 30, 2020

I actually need this event in some of my projects. However, I fire it from _beforeSave:

    protected function _beforeSave()
    {
        // ...
        // Just before exiting the function.
        if ($this->getStatus() != $this->getOrigData('status')) {
            Mage::dispatchEvent('order_status_changed_before_save', ['order' => $this]);
        }
        return $this;
    }

It doesn't check for state changes as condition of firing, which is less granular; a state may not have changed while status changes. However, you can easily check state changes in the observer.

I like firing in _beforeSave because I know at this point the states of the order model have settled, ie, the states of its attributes, the order items, order history, etc., have settled and are ready to be saved. I can also change the order's attributes in the observer and know that it will be saved.

@woutersamaey
Copy link
Contributor Author

Updated this PR to reflect @kiatng example. This is indeed better.

@woutersamaey woutersamaey changed the title New event "sales_order_state_change" New event "order_status_changed_before_save" Aug 26, 2020
Copy link
Collaborator

@luigifab luigifab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the new event into EVENTS.md.
Personally I would have used !=, but never mind.

app/code/core/Mage/Sales/Model/Order.php Outdated Show resolved Hide resolved
@fballiano
Copy link
Contributor

I think this one could be merged as it is (since it's not modifiable by other admins) and then I'll modify EVENTS.md in another PR.

@fballiano
Copy link
Contributor

oops, it's not mergeable because of conflicts and only @woutersamaey can fix them

@fballiano
Copy link
Contributor

completed via #2927

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Sales Relates to Mage_Sales enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants