扩展Mage_Sales_Model_Order

时间:2016-05-19 04:18:55

标签: php magento model magento-1.9 extend

我该如何开始? hrmmmm。 好的,现在我试图扩展mage_sales_mode_order。但是出现了这个错误。 致命错误:在第74行的/app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php中的非对象上调用成员函数load()

我不知道是什么问题因为我已经尝试使用相同的代码扩展不同的magento网站但是正在工作。 但以下是我的所作所为,并提前感谢。

MageCustom /销售/型号/ 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 /销售的/ 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>

2 个答案:

答案 0 :(得分:1)

问题

app/code/local中的active设置外,MageCustom_Sales.xml中的类和配置都正确,所以它们都是正确的:

<active>false</active>

将其打开,清除缓存并重试:)

测试

&GT;使用Magerun

如果你有magerun,你可以用它来测试你的重写:

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

如果您没有看到上述解决方案,则可能与重写有冲突,但您的重写配置错误的可能性更大。

如果你没有Magerun - 那就去吧!你不会后悔的。

&GT;没有Magerun

您可以将自定义脚本放到Magento项目的根目录中,只是为了测试类别名:

<?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)

希望这有帮助。

答案 1 :(得分:0)

确保您的自定义模块位于本地文件夹中,并通过在活动标记内插入true来启用模块。

<active>true</active>