Category Archives: Sales

○ Describe order creation in the admin
○ Describe the differences in order creation between the frontend and the admin:
■ Which classes are involved in order creation in the admin? What are their roles (especially the role of adminhtml classes)?
■ How does Magento calculate price when an order is created from the admin?
■ Which steps are necessary in order to create an order from the admin?
■ What happens when existing orders are edited in the admin?
■ What is the difference between order status and order state?
These code references can be used as an entry point to find answers to the questions above:
■ Mage_Sales_Model_Order
■ Mage_Sales_Model_Order_Address
■ Mage_Adminhtml_controllers_Order_CreateController ■ Mage_Adminhtml_Model_Sales_Order_Create
○ Card operations (capturing and authorization):
■ Which classes and methods are responsible for credit card operations (for example authorization or capturing)?
■ What is the difference between “pay” and “capture” operations?
■ What are the roles of the order, order_payment, invoice, and
payment methods in the process of charging a card?
■ What are the roles of the total models in the context of the invoice
object?
■ How does Magento store information about invoices?
These code references can be used as an entry point to find answers to the questions above:
■ Mage_Sales_Model_Order_Invoice
■ Mage/Sales/Model/Order/Invoice/*
■ Mage_Sales_Model_Order_Payment
■ Mage_Payment_Model_Method_Info
■ Mage_Payment_Model_Method_Abstract
○ Describe the order shipment structure and process:
■ How shipment templates be customized?
■ How can different items from a single order be shipped to multiple
addresses? Is it possible at all?
■ How does Magento store shipping and tracking information?
These code references can be used as an entry point to find answers to the questions above:
■ Mage_Sales_Model_Order_Shipment
■ Mage/Sales/Model/Order/Shipment/*
○ Describe the architecture and processing of refunds:
■ Which classes are involved, and which tables are used to store refund information?
■ How does Magento process taxes when refunding an order?
■ How does Magento process shipping fees when refunding an order?
■ What is the difference between online and offline refunding?
■ What is the role of the credit memo total models in Magento?
These code references can be used as an entry point to find answers to the questions above:
■ Mage_Sales_Model_Order_Creditmemo
■ Mage/Sales/Model/Order/Creditmemo/*
○ Describe the implementation of the three partial order operations (partial invoice, partial shipping, and partial refund):
■ How do partial order operations affect order state?
■ How is data for partial operations stored?
These code references can be used as an entry point to find answers to the questions above:
■ Mage_Sales_Model_Order_Invoice
■ Mage_Sales_Model_Order_Payment
■ Mage_Sales_Model_Order_Shipment
■ Mage_Sales_Model_Order_Creditmemo ■ Mage_Payment_Model_Method_Abstract
○ Describe cancel operations:
■ What cancel operations are available for the various order entities in Magento (order, order item, shipment, invoice, credit memo)? Do all of them support cancellation?
■ How are taxes processed during cancel operations?
These code references can be used as an entry point to find answers to the questions
above:
■ Mage_Adminhtml_Sales_OrderController
■ Mage_Sales_Model_Order_Invoice
■ Mage_Sales_Model_Order_Payment
■ Mage_Sales_Model_Order_Shipment
■ Mage_Sales_Model_Order_Creditmemo

Transactional Email – Adding Custom Function Output

Having the basics of Transactional Emails under one’s belt it is then safe to start taking things to the next level.

Consider the case of wanting to implement a new function that is currently not part of the basic order Transactional Email list.

In this case it is possible to rewrite the order model and implement your own functionality.

The creation of the necessary skeleton files are as follows:

/etc/module/{Namespace}_{Module}.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
    <modules>
        <{Namespace}_{Module}>
            <active>true</active>
            <codePool>local</codePool>
        </{Namespace}_{Module}>
    </modules>
</config>

/app/code/local/{Namespace}/{Module}/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <{Namespace}_{Module}>
            <version>0.1.0</version>
        </{Namespace}_{Module}>
    </modules>

    <global>
        <models>
           <core>
                <rewrite>
                    <email_template>{Namespace}_{Module}_Model_Email_Template</email_template>
                </rewrite>
            </core>
            <sales>
                <rewrite>
                    <order>{Namespace}_{Module}_Model_Email_Order</order>
                </rewrite>
            </sales>

            <emails>
                <class>{Namespace}_{Module}_Model</class>
            </emails>
        </models>

        <!-- Adds an entry into Transactional Email -->
        <template>
            <email>
                <!-- This value should reflect the path defined in the system.xml file: customer - order_confirmation_email - confirmation_template -->
                <customer_order_confirmation_email_confirmation_template translate="label" module="customer">
                    <label>Custom Order Confirmation</label>
                    <!-- Create file in the language locations at /app/locale/<language code>/email/order/confirmation.hmtl -->
                    <file>order/confirmation.html</file>
                    <type>html</type>
                </customer_order_confirmation_email_confirmation_template>
            </email>
        </template>
    </global>
</config>

/app/code/local/{Namespace}/{Module}/Model/Email/Order.php

<?php
class {Namespace}_{Module}_Model_Email_Order extends Mage_Sales_Model_Order {

	public function customFunction() {
		return 'This is a custom function return string';
	}
}

/app/code/local/{Namespace}/{Module}/Model/Email/Template.php

<?php
class {Namespace}_{Module}_Email_Template extends Mage_Core_Model_Email_Template {

	public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
	{
		$this->setSentSuccess(false);
		if (($storeId === null) && $this->getDesignConfig()->getStore()) {
			$storeId = $this->getDesignConfig()->getStore();
		}

		if (is_numeric($templateId)) {
			$this->load($templateId);
		} else {
			$localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
			$this->loadDefault($templateId, $localeCode);
		}

		if (!$this->getId()) {
			throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid transactional email code: ' . $templateId));
		}

		if (!is_array($sender)) {
			$this->setSenderName(Mage::getStoreConfig('trans_email/ident_' . $sender . '/name', $storeId));
			$this->setSenderEmail(Mage::getStoreConfig('trans_email/ident_' . $sender . '/email', $storeId));
		} else {
			$this->setSenderName($sender['name']);
			$this->setSenderEmail($sender['email']);
		}

		if (!isset($vars['store'])) {
			$vars['store'] = Mage::app()->getStore($storeId);
		}

		// Added code for custom var example.
		$vars['custom_var'] = 'This is a custom var';
		}

		$this->setSentSuccess($this->send($email, $name, $vars));
		return $this;
	}
}

Template definition (note that this was specified in the config.xml file above)
/app/locale/{language_code}/template/email/order/confirmation.html

<!--@subject {{var store.getFrontendName()}}: New Order # {{var order.increment_id}} @-->
<!--@vars
{"store url=\"\"":"Store Url",
"var logo_url":"Email Logo Image Url",
"var logo_alt":"Email Logo Image Alt",
"htmlescape var=$order.getCustomerName()":"Customer Name",
"var store.getFrontendName()":"Store Name",
"store url=\"customer/account/\"":"Customer Account Url",
"var order.increment_id":"Order Id",
"var order.getCreatedAtFormated('long')":"Order Created At (datetime)",
"var order.getBillingAddress().format('html')":"Billing Address",
"var payment_html":"Payment Details",
"var order.getShippingAddress().format('html')":"Shipping Address",
"var order.getShippingDescription()":"Shipping Description",
"layout handle=\"sales_email_order_items\" order=$order":"Order Items Grid",
"var order.getEmailCustomerNote()":"Email Order Note"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">

    {{var order.customFunction()}} <!-- as defined in /app/code/local/{Namespace}/{Module}/Model/Email/Order.php -->
    {{var custom_var}} <!-- as defined in /app/code/local/{Namespace}/{Module}/Model/Email/Template.php -->

    <table cellspacing="0" cellpadding="0" border="0" width="100%">
        <tr>
            <td align="center" valign="top" style="padding:20px 0 20px 0">
                <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                    <!-- [ header starts here] -->
                    <tr>
                        <td valign="top"><a href="{{store url=""}}"><img src="{{var logo_url}}" alt="{{var logo_alt}}" style="margin-bottom:10px;" border="0"/></a></td>
                    </tr>
                    <!-- [ middle starts here] -->
                    <tr>
                        <td valign="top">
                            <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;"">Hello, {{htmlescape var=$order.getCustomerName()}}</h1>
                            <p style="font-size:12px; line-height:16px; margin:0;">
                                Thank you for your order from {{var store.getFrontendName()}}.
                                Once your package ships we will send an email with a link to track your order.
                                You can check the status of your order by <a href="{{store url="customer/account/"}}" style="color:#1E7EC8;">logging into your account</a>.
                                If you have any questions about your order please contact us at <a href="mailto:{{config path='trans_email/ident_support/email'}}" style="color:#1E7EC8;">{{config path='trans_email/ident_support/email'}}</a> or call us at <span class="nobr">{{config path='general/store_information/phone'}}</span> Monday - Friday, 8am - 5pm PST.
                            </p>
                            <p style="font-size:12px; line-height:16px; margin:0;">Your order confirmation is below. Thank you again for your business.</p>
                    </tr>
                    <tr>
                        <td>
                            <h2 style="font-size:18px; font-weight:normal; margin:0;">Your Order #{{var order.increment_id}} <small>(placed on {{var order.getCreatedAtFormated('long')}})</small></h2>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table cellspacing="0" cellpadding="0" border="0" width="650">
                                <thead>
                                <tr>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Billing Information:</th>
                                    <th width="10"></th>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Payment Method:</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var order.getBillingAddress().format('html')}}
                                    </td>
                                    <td>&nbsp;</td>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var payment_html}}
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <br/>
                            {{depend order.getIsNotVirtual()}}
                            <table cellspacing="0" cellpadding="0" border="0" width="650">
                                <thead>
                                <tr>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Shipping Information:</th>
                                    <th width="10"></th>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Shipping Method:</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var order.getShippingAddress().format('html')}}
                                        &nbsp;
                                    </td>
                                    <td>&nbsp;</td>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var order.getShippingDescription()}}
                                        &nbsp;
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <br/>
                            {{/depend}}
                            {{layout handle="sales_email_order_items" order=$order}}
                            <p style="font-size:12px; margin:0 0 10px 0">{{var order.getEmailCustomerNote()}}</p>
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you, <strong>{{var store.getFrontendName()}}</strong></p></center></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</div>
</body>

Having created the module it is then time to create the Transactional Email in the administrator of Magento.
Magento Admin Panel -> System -> Transactional Emails -> Add New Template
The Template select should now allow you to select the template that you have created, it will be visible from the name defined in the config.xml file ().

Once this Template has been added, it can then be set as the default order template.

To achieve this then select magento Admin Panel -> System -> Configuration -> Sales (Sales Emails)
Then expand the Order panel. In the New Order Confirmation select, choose the name that was defined in the previous step when the new template was defined.

That will now give you the opportunity to use custom variables and custom functions in your order transactional emails.