Engine23
blogBannerImage
By Engine23

Rewrite or Overload Mage_Sales_Model_Order

If you have ever needed to rewrite/overload a core magento Model.

Its actually pretty easy

Step 1: create the module instantiation file

app/etc/modules/Russellalbin_Sales.xml

<?xml version="1.0"?>
<config>
<modules>
<Indiebooker_Sales>
<active>true</active>
<codePool>local</codePool>
</Indiebooker_Sales>
</modules>
</config>

Step 2: create the folders

app/code/local/Russellalbin/Sales/

Then create two sub folders

app/code/local/Russellalbin/Sales/etc

Inside etc, create a config.xml

app/code/local/Russellalbin/Sales/Model

Inside Model, create a file Order.php

Your file:

app/code/local/Russellalbin/Sales/etc/config.xml should look similar to:

<?xml version="1.0"?>
    
<config>
<modules>
<Russellalbin_Sales>
<version>0.1.0</version>
</Russellalbin_Sales>
</modules>
<global>
<models>
<sales>
<rewrite>
<order>Russellalbin_Sales_Model_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>

Your Order.php should look similar to:


    
<?php

class Russellalbin_Sales_Model_Order extends Mage_Sales_Model_Order
{

/**
* Send email with order data
*
* @return Mage_Sales_Model_Order
*/
public function sendNewOrderEmail()
{
$storeId = $this->getStore()->getId();

if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
return $this;
}
// Get the destination email addresses to send copies to
$copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);

// Start store emulation process
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

try {
// Retrieve specified view block from appropriate design package (depends on emulated store)
$paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($storeId);
$paymentBlockHtml = $paymentBlock->toHtml();
} catch (Exception $exception) {
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
throw $exception;
}

// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

// Retrieve corresponding email template id and customer name
if ($this->getCustomerIsGuest()) {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
$customerName = $this->getBillingAddress()->getName();
} else {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
$customerName = $this->getCustomerName();
}

$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($this->getCustomerEmail(), $customerName);
if ($copyTo && $copyMethod == 'bcc') {
// Add bcc to customer email
foreach ($copyTo as $email) {
$emailInfo->addBcc($email);
}
}
$mailer->addEmailInfo($emailInfo);

// Email copies are sent as separated emails if their copy method is 'copy'
if ($copyTo && $copyMethod == 'copy') {
foreach ($copyTo as $email) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($email);
$mailer->addEmailInfo($emailInfo);
}
}

// Get the customer to get a custom attribute, favorite baseball team
$customer = $this->getCustomer();
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
'favofite_baseball_team' => $customer->getData('favorite_baseball_team')
)
);
$mailer->send(); $this->setEmailSent(true);
$this->_getResource()->saveAttribute($this, 'email_sent');

return $this;
}

}