I am working on this Magento 2 extension and part of the features is need to entry in another table where order total will be inserted. so what I need is to find order invoice status paid event observer.How can I get order invoice status paid event in observer?
Magento – observer event after order invoice status paid executed in magento 2
event-observerinvoicemagento-2.1.3sales-order
Related Solutions
I use as follows:
1 - app/code/loca/Packagename/Modulename/etc/config.xml
<config>
<global>
<models>
<sales>
<rewrite>
<order>Packagename_Modulename_Model_Rewrite_Order</order>
</rewrite>
</sales>
<sales_resource>
<rewrite>
<quote>Packagename_Modulename_Model_Rewrite_Resource_Sales_Quote</quote>
</rewrite>
</sales_resource>
<modelrewrite>
<class>Packagename_Modulename_Model_Rewrite</class>
</modelrewrite>
</models>
<events>
<packagename_modulename_order_status_change>
<observers>
<packagename_modulename_model_rewrite>
<type>singleton</type>
<class>modelrewrite/observer</class>
<method>changeStateOrder</method>
</packagename_modulename_model_rewrite>
</observers>
</packagename_modulename_order_status_change>
</events>
<resources>
<modulename_setup>
<setup>
<module>Packagename_Modulename</module>
<class>Mage_Sales_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</modulename_setup>
</resources>
</global>
<crontab>
<!-- here I am listing only major routines -->
<jobs>
<Packagename_Modulename_Model_Rewrite_SendOrderToERP>
<schedule>
<cron_expr>12 * * * *</cron_expr>
</schedule>
<run>
<model>modelrewrite/observer::runSendOrderToERP</model>
</run>
</Packagename_Modulename_Model_Rewrite_SendOrderToERP>
<Packagename_Modulename_Model_Rewrite_ReceiveERPStatusOrder>
<schedule>
<cron_expr>*/5 * * * *</cron_expr>
</schedule>
<run>
<model>modelrewrite/observer::runReceiveERPStatusOrder</model>
</run>
</Packagename_Modulename_Model_Rewrite_ReceiveERPStatusOrder>
</jobs>
</crontab>
2 - app/code/loca/Packagename/Modulename/Model/Rewrite/Order.php
class Packagename_Modulename_Model_Rewrite_Order extends Mage_Sales_Model_Order{
public function _setState($state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = false){
// attempt to set the specified state
// tenta definir o status especificado
if ($shouldProtectState) {
if ($this->isStateProtected($state)) {
Mage::throwException(
Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state)
);
}
}
$this->setData('state', $state);
// add status history
if ($status) {
if ($status === true) {
$status = $this->getConfig()->getStateDefaultStatus($state);
}
$this->setStatus($status);
$history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
$history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
}
Mage::dispatchEvent('packagename_modulename_order_status_change', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified));
Mage::log("Packagename_Modulename_Model_Rewrite_Order Changing order to STATE ".$state." STATUS ".$status);
return $this;
}
}
3 - app/code/loca/Packagename/Modulename/Model/Rewrite/Observer.php
class Packagename_modulename_Model_Rewrite_Observer{
public function implementOrderStatus($event){
$order = $event->getOrder();
$state = $event->getState();
$status = $event->getStatus();
return $this;
}
public function changeStateOrder($observer){
Mage::log('changeStateOrder');
$order = $observer->getEvent()->getOrder();
if($order->getStatus() == "processing_paid"){
$this->autoInvoicing($order);
}
else if($order->getStatus() == "processing_shipped"){
$this->autoShipment($order);
}
// use your necessary ifs
$this->sendTransactionalEmail($order);
return $this;
}
public function autoShipment(Mage_Sales_Model_Order $order){
//...
}
public function autoInvoicing(Mage_Sales_Model_Order $order){
//...
//return $this;
}
public function sendTransactionalEmail(Mage_Sales_Model_Order $order){
switch ($order->getStatus()) {
case "processing":
$templateId = 1;
break;
case "pending_payment":
$templateId = 0;
break;
//...
default:
$templateId = 0;
break;
}
//...
}
public function runSendOrderToERP($schedule){
//...
}
public function runReceiveERPStatusOrder($schedule){
//...
}
//And much more another methods can be used after change state / status order: refund, calcel, etc
Override magento Mage_Sales_Model_Order using or Copy
app/code/core/Mage/Sales/Model/Order.php
to
app/code/local/Mage/Sales/Model/Order.php
add define new event on _setStatus function
protected function _setState($state, $status = false, $comment = '',
$isCustomerNotified = null, $shouldProtectState = false)
{
Mage::dispatchEvent('sales_order_status_before', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState));
// attempt to set the specified state
if ($shouldProtectState) {
if ($this->isStateProtected($state)) {
Mage::throwException(
Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state)
);
}
}
$this->setData('state', $state);
// add status history
if ($status) {
if ($status === true) {
$status = $this->getConfig()->getStateDefaultStatus($state);
}
$this->setStatus($status);
$history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
$history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
}
Mage::dispatchEvent('sales_order_status_after', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState));
return $this;
}
using two sales_order_status_after and sales_order_status_before you can do your requirement
or best way to do this sales_order_save_after and in this observer if
you get old value then try
$oldstatus=$order->getOrigData('status');
$Newstatus=$order->getData('status');
https://stackoverflow.com/questions/8182009/get-product-changes/8184430#8184430
Best Answer
Here is the list of Magento 2 events.
https://cyrillschumacher.com/magento2-list-of-all-dispatched-events/
In your case you can use
sales_order_invoice_pay
event which is calling from below location inside pay() function.