-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Riskified/cancelation-email-when-order-is…
…-declined Cancelation email when order is declined
- Loading branch information
Showing
7 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
app/code/community/Riskified/Full/Model/Observer/Order/Decline.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
class Riskified_Full_Model_Observer_Order_Decline | ||
{ | ||
private $order; | ||
public function handleOrderDecline( | ||
Varien_Event_Observer $observer | ||
) { | ||
$order = $observer->getOrder(); | ||
$this->order = $order; | ||
/** | ||
* @var Riskified_Full_Helper_Data $dataHelper | ||
*/ | ||
$dataHelper = Mage::helper("full"); | ||
|
||
if (!$dataHelper->isDeclineNotificationEnabled()) { | ||
return $this; | ||
} | ||
if (Mage::registry("decline-email-sent")) { | ||
return $this; | ||
} | ||
|
||
Mage::register("decline-email-sent", true); | ||
|
||
$emailTemplate = Mage::getModel('core/email_template') | ||
->loadDefault('riskified_order_declined'); | ||
|
||
$emailTemplate->setSenderEmail( | ||
$dataHelper->getDeclineNotificationSenderEmail() | ||
); | ||
|
||
$emailTemplate->setSenderName( | ||
$dataHelper->getDeclineNotificationSenderName() | ||
); | ||
|
||
$subject = $dataHelper->getDeclineNotificationSubject(); | ||
$content = $dataHelper->getDeclineNotificationContent(); | ||
|
||
$shortCodes = array( | ||
"{{customer_name}}", | ||
"{{customer_firstname}}", | ||
"{{order_increment_id}}", | ||
"{{order_view_url}}", | ||
"{{products}}", | ||
"{{store_name}}", | ||
); | ||
$formattedPayload = $this->getFormattedData(); | ||
|
||
foreach ($shortCodes as $key => $value) { | ||
$subject = str_replace($value, $formattedPayload[$key], $subject); | ||
$content = str_replace($value, $formattedPayload[$key], $content); | ||
} | ||
|
||
try { | ||
if ($content == "") { | ||
throw new Exception("Email content is empty"); | ||
} | ||
|
||
if ($subject == "") { | ||
throw new Exception("Email subject is empty"); | ||
} | ||
|
||
$wasSent = $emailTemplate->send( | ||
$order->getCustomerEmail(), | ||
$order->getCustomerName(), | ||
array( | ||
'store' => Mage::app()->getStore(), | ||
'subject' => $subject, | ||
'order' => $order, | ||
'content' => $content | ||
) | ||
); | ||
|
||
if ($wasSent === true) { | ||
$fileLog = $dataHelper->__( | ||
"Decline email was sent to customer %s (%s) for order #%s", | ||
$order->getCustomerName(), | ||
$order->getCustomerEmail(), | ||
$order->getIncrementId() | ||
); | ||
|
||
$orderComment = $dataHelper->__( | ||
"Decline email was sent to customer %s (%s)", | ||
$order->getCustomerName(), | ||
$order->getCustomerEmail() | ||
); | ||
} else { | ||
$fileLog = $dataHelper->__( | ||
"Decline email was not sent to customer %s (%s) for order #%s - server internal error", | ||
$order->getCustomerName(), | ||
$order->getCustomerEmail(), | ||
$order->getIncrementId() | ||
); | ||
$orderComment = $dataHelper->__( | ||
"Decline email was not sent to customer %s (%s) - server internal error", | ||
$order->getCustomerName(), | ||
$order->getCustomerEmail() | ||
); | ||
} | ||
|
||
Mage::helper('full/log')->log($fileLog); | ||
|
||
$order | ||
->addStatusHistoryComment($orderComment) | ||
->setIsCustomerNotified(true); | ||
$order->save(); | ||
} catch (Exception $e) { | ||
Mage::logException($e); | ||
} | ||
} | ||
|
||
private function getFormattedData() | ||
{ | ||
$products = array(); | ||
|
||
foreach ($this->order->getAllItems() as $item) { | ||
$products[] = $item->getName(); | ||
} | ||
|
||
$data = array( | ||
$this->order->getCustomerName(), | ||
$this->order->getCustomerFirstname(), | ||
$this->order->getIncrementId(), | ||
Mage::getUrl('sales/order/view', array('order_id' => $this->order->getId())), | ||
join(', ', $products), | ||
Mage::app()->getStore()->getFrontendName() | ||
); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!--@subject {{var subject}} @--> | ||
<!--@vars | ||
{"store url=\"\"":"Store Url", | ||
"var logo_url":"Email Logo Image Url", | ||
"var logo_alt":"Email Logo Image Alt", | ||
"htmlescape var=$order.getCustomerName()":"Customer Name", | ||
"var store.getFrontendName()":"Store Name", | ||
"store url=\"customer/account/\"":"Customer Account Url", | ||
"var order.increment_id":"Order Id", | ||
"var order.getCreatedAtFormated('long')":"Order Created At (datetime)", | ||
"var order.getBillingAddress().format('html')":"Billing Address", | ||
"var payment_html":"Payment Details", | ||
"var order.getShippingAddress().format('html')":"Shipping Address", | ||
"var order.getShippingDescription()":"Shipping Description", | ||
"layout handle=\"sales_email_order_items\" order=$order":"Order Items Grid", | ||
"var order.getEmailCustomerNote()":"Email Order Note"} | ||
@--> | ||
<!--@styles | ||
@--> | ||
|
||
{{template config_path="design/email/header"}} | ||
{{inlinecss file="email-inline.css"}} | ||
|
||
<table cellpadding="0" cellspacing="0" border="0"> | ||
|
||
<tr class="order-information"> | ||
<td> | ||
{{var content}} | ||
</td> | ||
</tr> | ||
</table> | ||
|
||
{{template config_path="design/email/footer"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters