0

When I generate my invoice and try to save I get a error,

Fatal error: Call to a member function getId() on null in /var/www/html/app/code/local/Mage/Sales/Order/Invoice.php:835"

And my clients can't complete the order.

enter image description here

public function sendEmail($notifyCustomer = true, $comment = '')
{
    $order = $this->getOrder();
    $storeId = $order->getStore()->getId();

    if (!Mage::helper('sales')->canSendNewInvoiceEmail($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);
    // Check if at least one recepient is found
    if (!$notifyCustomer && !$copyTo) {
        return $this;
    }

    // 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($order->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 ($order->getCustomerIsGuest()) {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
        $customerName = $order->getBillingAddress()->getName();
    } else {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
        $customerName = $order->getCustomerName();
    }

    $mailer = Mage::getModel('core/email_template_mailer');
    if ($notifyCustomer) {
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($order->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' or a customer should not be notified
    if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
        foreach ($copyTo as $email) {
            $emailInfo = Mage::getModel('core/email_info');
            $emailInfo->addTo($email);
            $mailer->addEmailInfo($emailInfo);
        }
    }

    // Set all required params and send emails
    // ERROR LINE BELOW
    $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
    $mailer->setStoreId($storeId);
    $mailer->setTemplateId($templateId);
    $mailer->setTemplateParams(array(
            'order'        => $order,
            'invoice'      => $this,
            'comment'      => $comment,
            'billing'      => $order->getBillingAddress(),
            'payment_html' => $paymentBlockHtml
        )
    );
    $mailer->send();
    $this->setEmailSent(true);
    $this->_getResource()->saveAttribute($this, 'email_sent');

    return $this;
}

Does anyone have an idea about this?

7
  • can you share Invoice.php file code?
    – Abdul
    Apr 9, 2018 at 13:31
  • @Pedro Albuquerque, Please mention the Magento shop version
    – Sankar_k
    Apr 9, 2018 at 13:34
  • @Sankar_k My Magento shop version is 1.9.2.4 Apr 9, 2018 at 13:42
  • Ok, share the issue occur file (Invoice.php)
    – Sankar_k
    Apr 9, 2018 at 13:47
  • @Sankar_k I just checked the code of my Invoice.php and the source code of Magento 1.9.2.4 Invoice.php and they're the same. Apr 9, 2018 at 14:00

1 Answer 1

0

Order object doesnt exist at time of calling function (this usually happens if function is called multiple times, e.g. by a magento event hook)

Try adding if (!$order) { return $this; } as below;

public function sendEmail($notifyCustomer = true, $comment = '')
{
    $order = $this->getOrder();

    if (!$order) { return $this; }

    $storeId = $order->getStore()->getId();

    if (!Mage::helper('sales')->canSendNewInvoiceEmail($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);
    // Check if at least one recepient is found
    if (!$notifyCustomer && !$copyTo) {
        return $this;
    }

    // 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($order->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 ($order->getCustomerIsGuest()) {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
        $customerName = $order->getBillingAddress()->getName();
    } else {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
        $customerName = $order->getCustomerName();
    }

    $mailer = Mage::getModel('core/email_template_mailer');
    if ($notifyCustomer) {
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($order->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' or a customer should not be notified
    if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
        foreach ($copyTo as $email) {
            $emailInfo = Mage::getModel('core/email_info');
            $emailInfo->addTo($email);
            $mailer->addEmailInfo($emailInfo);
        }
    }

    // Set all required params and send emails
    // ERROR LINE BELOW
    $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));Salescast 
    $mailer->setStoreId($storeId);
    $mailer->setTemplateId($templateId);
    $mailer->setTemplateParams(array(
            'order'        => $order,
            'invoice'      => $this,
            'comment'      => $comment,
            'billing'      => $order->getBillingAddress(),
            'payment_html' => $paymentBlockHtml
        )
    );
    $mailer->send();
    $this->setEmailSent(true);
    $this->_getResource()->saveAttribute($this, 'email_sent');

    return $this;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.