1

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" /> </event> 

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?

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

2 Answers 2

0

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

sales_order_invoice_pay sales_order_invoice_save_after sales_order_payment_place_end 
0

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

app/code/Company/Module/etc/adminhtml/di.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Sales\Api\OrderRepositoryInterface"> <plugin name="change_user_group" type="Company\Module\Model\Plugin\ChangeUserGroupPlugin"/> </type> </config> 

Your Plugin:

<?php 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, $result ) { 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) { $customer->setGroupId(self::ID_CUSTOMER_GROUP_CHANGE); $this->_customerRepository->save($customer); } } catch (\Exception $e) { throw $e; } } return $result; } } 
3
  • 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? Commented Jan 13, 2017 at 9:26
  • It gives new order status This if ($order->getState() === Order::STATE_COMPLETE) { is not working 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.