扩展 Mage_Sales_Model_Order
Extend Mage_Sales_Model_Order
我该如何开始?嗯嗯。
好的,现在我正在尝试扩展 mage_sales_mode_order。但出现此错误。
致命错误:在第 74 行 /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php 中的非对象上调用成员函数 load()
我不知道是什么问题,因为我已经尝试使用相同的代码扩展不同的 magento 站点,但正在运行。
但以下是我所做的,在此先感谢。
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>
问题
你的 class 和配置都是正确的,假设它们存在于 app/code/local
中,除了 MageCustom_Sales.xml
中的 active
设置:
<active>false</active>
打开它,清除缓存并重试:)
测试
> 与马格润
如果你有 magerun,你可以用它来测试你的重写:
n98-magerun.phar dev:class:lookup model sales/order
# Model sales/order resolves to MageCustom_Sales_Model_Order
如果没有看到上面的解析,可能是你的rewrite有冲突,但更有可能是你的rewrite配置有误。
如果您没有 Magerun - 获取它!你不会后悔的。
> 没有马格伦
您可以将自定义脚本放入 Magento 项目的根目录中,以测试 class 别名:
<?php
include 'app/Mage.php';
Mage::app();
var_dump(get_class(Mage::getModel('sales/order')));
运行 通过 php myTestFile.php
从命令行,或通过 http://magento.local/myTestFile.php
.
从浏览器
如果有效,您应该会看到 string(28) "MageCustom_Sales_Model_Order"
。如果没有,您会看到 bool(false)
.
希望这对您有所帮助。
确保您的自定义模块位于本地文件夹中,并通过在活动标记中插入 true 来启用您的模块。
<active>true</active>
我该如何开始?嗯嗯。 好的,现在我正在尝试扩展 mage_sales_mode_order。但出现此错误。 致命错误:在第 74 行 /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php 中的非对象上调用成员函数 load()
我不知道是什么问题,因为我已经尝试使用相同的代码扩展不同的 magento 站点,但正在运行。 但以下是我所做的,在此先感谢。
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>
问题
你的 class 和配置都是正确的,假设它们存在于 app/code/local
中,除了 MageCustom_Sales.xml
中的 active
设置:
<active>false</active>
打开它,清除缓存并重试:)
测试
> 与马格润
如果你有 magerun,你可以用它来测试你的重写:
n98-magerun.phar dev:class:lookup model sales/order # Model sales/order resolves to MageCustom_Sales_Model_Order
如果没有看到上面的解析,可能是你的rewrite有冲突,但更有可能是你的rewrite配置有误。
如果您没有 Magerun - 获取它!你不会后悔的。
> 没有马格伦
您可以将自定义脚本放入 Magento 项目的根目录中,以测试 class 别名:
<?php
include 'app/Mage.php';
Mage::app();
var_dump(get_class(Mage::getModel('sales/order')));
运行 通过 php myTestFile.php
从命令行,或通过 http://magento.local/myTestFile.php
.
如果有效,您应该会看到 string(28) "MageCustom_Sales_Model_Order"
。如果没有,您会看到 bool(false)
.
希望这对您有所帮助。
确保您的自定义模块位于本地文件夹中,并通过在活动标记中插入 true 来启用您的模块。
<active>true</active>