Trigger event after an order has been created/saved

<event name="sales_order_place_after">
    <observer name="change_user_group" instance="Custom\Module\Observer\ChangeUserGroupObserver" />

We have 2 events sales_order_place_after or checkout_onepage_controller_success_action.

What if user has refreshed the success page & if group is already changed then i don't want to call that observer.

It should not go to observer file itself.

Can we put like if condition on events.xml? What is alternative?

  • See my updated answer. We should try with Plugin. Commented Feb 27, 2017 at 14:55

2 Answers 2


If you want after payment is completed successfully then you should try one of the following:


We can use sales_order_save_after, check the condition directly on your observer. However, we should try with Plugin.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="change_user_group"


Your Plugin:


namespace Company\Module\Model\Plugin;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Sales\Model\Order;

class ChangeUserGroupPlugin

    const ID_CUSTOMER_GROUP_CHANGE = 5; // For example group

     * @var CustomerRepositoryInterface
    protected $_customerRepository;

     * ChangeUserGroupObserver constructor.
     * @param CustomerRepositoryInterface $customerRepository
    public function __construct(
        CustomerRepositoryInterface $customerRepository
        $this->_customerRepository     = $customerRepository;

     * @param \Magento\Sales\Api\OrderRepositoryInterface $subject
     * @param \Magento\Sales\Api\Data\OrderInterface $result
     * @return mixed
     * @throws \Exception
    public function afterSave(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
    ) {
        if($result->getState() == Order::STATE_COMPLETE) {
            try {
                $customerId    = $result->getCustomerId();
                $customer      = $this->_customerRepository->getById($customerId);
                $current_group = $customer->getGroupId();
                if ($current_group != self::ID_CUSTOMER_GROUP_CHANGE) {
            catch (\Exception $e) {
                throw $e;
        return $result;
  • In observer i already checked. Don't want to enter in Observer. But using sales_order_save_after if payment failed then also it changes the customer group w/o placing successfully order right?
    – Jackson
    Commented Jan 13, 2017 at 9:26
  • It gives new order status This if ($order->getState() === Order::STATE_COMPLETE) { is not working
    – Jackson
    Commented Jan 16, 2017 at 6:46
  • @AnkitShah you can check the state of order. In case, new order status, we need to check if(in_array($order->getState(), ['new', 'completed'])) Commented Jan 16, 2017 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.