Mohd Nizam Mohd Nizam - 6 months ago 288
PHP Question

Extend Mage_Sales_Model_Order

how do i start ? hrmmmm.
ok , right now Im trying to extend the mage_sales_mode_order . but this error appear.
Fatal error: Call to a member function load() on a non-object in /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php on line 74

i don't know what is the problem because i already try using the same code extend different magento site but is working.
but below is what that i do and thanks in advance.

MageCustom/Sales/Model/Order.php

class MageCustom_Sales_Model_Order extends Mage_Sales_Model_Order
{

public function queueNewOrderEmail($forceMode = false)
{
$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
/** @var $appEmulation Mage_Core_Model_App_Emulation */
$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();
}

/** @var $mailer Mage_Core_Model_Email_Template_Mailer */
$mailer = Mage::getModel('core/email_template_mailer');
/** @var $emailInfo Mage_Core_Model_Email_Info */
$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);
}
}

// 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
));

/** @var $emailQueue Mage_Core_Model_Email_Queue */
$emailQueue = Mage::getModel('core/email_queue');
$emailQueue->setEntityId($this->getId())
->setEntityType(self::ENTITY)
->setEventType(self::EMAIL_EVENT_NAME_NEW_ORDER)
->setIsForceCheck(!$forceMode);

//$mailer->setQueue($emailQueue)->send();
$mailer->send();
Mage::log('new order trigger email', null, 'email.log');

$this->setEmailSent(true);
$this->_getResource()->saveAttribute($this, 'email_sent');

return $this;
}


MageCustom/Sales/etc/config.xml

<config>
<modules>
<MageCustom_Sales>
<version>0.1.0</version>
</MageCustom_Sales>
</modules>
<global>
<models>
<MageCustom_Sales>
<class>MageCustom_Sales_Model</class>
</MageCustom_Sales>
<sales>
<rewrite>
<order>MageCustom_Sales_Model_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>


MageCustom_Sales.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MageCustom_Sales>
<active>false</active>
<codePool>local</codePool>
</MageCustom_Sales>
</modules>
</config>

Answer

Problem

Your class and configuration are all correct assuming they exist in app/code/local, except for the active setting in MageCustom_Sales.xml:

<active>false</active>

Turn it on, clear your cache and try again :)


Testing

> With Magerun

If you've got magerun, you can use it to test your rewrite:

n98-magerun.phar dev:class:lookup model sales/order
# Model sales/order resolves to MageCustom_Sales_Model_Order

If you don't see the above resolution, you may have conflicts with the rewrite, but it's more likely that your rewrite configuration is wrong.

If you don't have Magerun - get it! You won't regret it.

> Without Magerun

You can drop a custom script into the root directory of your Magento project just to test the class alias:

<?php
include 'app/Mage.php';
Mage::app();
var_dump(get_class(Mage::getModel('sales/order')));

Run it from command line via php myTestFile.php, or from browser via http://magento.local/myTestFile.php.

If it works, you should see string(28) "MageCustom_Sales_Model_Order". If it doesn't you'll see bool(false).

Hope this helps.