Skip to content

Commit

Permalink
Merge pull request #1199 from dfry22/sales_order_state_change_before
Browse files Browse the repository at this point in the history
Add Event for sales_order_state_change_before during Order->saveState()
  • Loading branch information
vpelipenko committed Apr 30, 2015
2 parents 78f470b + 26b21de commit 86ee3db
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions app/code/Magento/Sales/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,13 +920,16 @@ public function getShippingAddress()
* Order state setter.
* If status is specified, will add order status history with specified comment
* the setData() cannot be overridden because of compatibility issues with resource model
* By default allows to set any state. Can also update status to default or specified value
* Complete and closed states are encapsulated intentionally
*
* @param string $state
* @param string|bool $status
* @param string $comment
* @param bool $isCustomerNotified
* @param bool $shouldProtectState
* @return \Magento\Sales\Model\Order
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setState(
$state,
Expand All @@ -935,29 +938,7 @@ public function setState(
$isCustomerNotified = null,
$shouldProtectState = true
) {
return $this->_setState($state, $status, $comment, $isCustomerNotified, $shouldProtectState);
}

/**
* Order state protected setter.
* By default allows to set any state. Can also update status to default or specified value
* Complete and closed states are encapsulated intentionally, see the _checkState()
*
* @param string $state
* @param string|bool $status
* @param string $comment
* @param bool $isCustomerNotified
* @param bool $shouldProtectState
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _setState(
$state,
$status = false,
$comment = '',
$isCustomerNotified = null,
$shouldProtectState = false
) {
// attempt to set the specified state
if ($shouldProtectState) {
if ($this->isStateProtected($state)) {
Expand All @@ -966,17 +947,29 @@ protected function _setState(
);
}
}
$this->setData('state', $state);

$transport = new \Magento\Framework\Object(
[
'state' => $state,
'status' => $status,
'comment' => $comment,
'is_customer_notified' => $isCustomerNotified
]
);

$this->_eventManager->dispatch('sales_order_state_change_before', array('order' => $this, 'transport' => $transport));
$status = $transport->getStatus();
$this->setData('state', $transport->getState());

// add status history
if ($status) {
if ($status === true) {
$status = $this->getConfig()->getStateDefaultStatus($state);
$status = $this->getConfig()->getStateDefaultStatus($transport->getState());
}
$this->setStatus($status);
$history = $this->addStatusHistoryComment($comment, false);
$history = $this->addStatusHistoryComment($transport->getComment(), false);
// no sense to set $status again
$history->setIsCustomerNotified($isCustomerNotified);
$history->setIsCustomerNotified($transport->getIsCustomerNotified());
}
return $this;
}
Expand Down Expand Up @@ -1167,7 +1160,7 @@ public function registerCancellation($comment = '', $graceful = true)
$this->setTotalCanceled($this->getGrandTotal() - $this->getTotalPaid());
$this->setBaseTotalCanceled($this->getBaseGrandTotal() - $this->getBaseTotalPaid());

$this->_setState($cancelState, true, $comment);
$this->setState($cancelState, true, $comment, null, false);
} elseif (!$graceful) {
throw new \Magento\Framework\Exception\LocalizedException(__('We cannot cancel this order.'));
}
Expand Down

0 comments on commit 86ee3db

Please sign in to comment.