0

Does someone know where the value of {{var order.increment_id}} comes from?

The variable is in the “credit memo” template in the transactional email section.

Can someone help? Because I need to create a function and variable to apply in the transactional email but don’t where to create it.

Thank you very much.

0

Open following class:

app/code/core/Mage/Sales/Model/Order/Creditmemo.php

Check following code snippet, how magento pass the order to an email template.

$mailer->setTemplateParams(array(
        'order'        => $order,
        'creditmemo'   => $this,
        'comment'      => $comment,
        'billing'      => $order->getBillingAddress(),
        'payment_html' => $paymentBlockHtml
    )
);

[Update]

Overwrite Creditmemo class:

<global>
    <models>
        <sales>
            <rewrite>
                <order_creditmemo>SR_MagentoCommunity_Model_Order_Creditmemo</order_creditmemo>
            </rewrite>
        </sales>
    </models>
</global>

app/code/local/SR/MagentoCommunity/Model/Order/Creditmemo.php

<?php

class SR_MagentoCommunity_Model_Order_Creditmemo extends Mage_Sales_Model_Order_Creditmemo
{
    /**
     * Send email with creditmemo data
     *
     * @param boolean $notifyCustomer
     * @param string $comment
     * @return Mage_Sales_Model_Order_Creditmemo
     */
    public function sendEmail($notifyCustomer = true, $comment = '')
    {
        $order = $this->getOrder();
        $storeId = $order->getStore()->getId();

        if (!Mage::helper('sales')->canSendNewCreditmemoEmail($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);
            }
        }

        $newVarObject = new Varien_Object();
        $newVarObject->setData(['new_var' => 'New Var Value']);

        // 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'        => $order,
                'creditmemo'   => $this,
                'comment'      => $comment,
                'billing'      => $order->getBillingAddress(),
                'payment_html' => $paymentBlockHtml,
                'newVarObject' => $newVarObject
            )
        );
        $mailer->send();
        $this->setEmailSent(true);
        $this->_getResource()->saveAttribute($this, 'email_sent');

        return $this;
    }

    public function getNewVarFunction()
    {
        return $this->getIncrementId().'_Modified';
    }
}

Now in the email template, you can use the following variable:

{{var creditmemo.new_var_function}}

{{var newVarObject.new_var}}

  • I couldnt find that file. Do you think that is because I'm on Magento 1.9? – dragon25 May 31 at 13:59
  • After Vendor, there is no magento folder. – dragon25 May 31 at 13:59
  • Check updated answer, this is M1 now. – Sohel Rana May 31 at 14:59
  • Should I create the function and variables there in order to use it in the template? Can you give me an example, I'll be very grateful. Thanks – dragon25 May 31 at 15:34
  • Check updated answer – Sohel Rana May 31 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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