Customizing and Styling Magento Order Confirmation

Posted on Monday, July 23rd, 2012

Magento is powerful, but man does it make some things more laborious than they need to be.

Out of the box, Magento’s order confirmation is weak. The client wanted a more robust solution, and after scraping the web for resources and dumping class methods out, I was able to put together a very robust solution.

What to Edit

First, you should know that all of this takes place in the your template file located here: app/design/frontend/[package]/[theme]/template/checkout/success.phtml

How to Test

Second, testing the checkout can be daunting – unless you know a couple of little tips that make a big difference:

  1. Set your payment method to Test in your dashboard. Then you can use a test credit card to checkout and get the success page to display. (FYI, the Visa test card number is 4111111111111111)
  2. Temporarily edit a Magento Core code file to disable clearing session. In app/code/core/Mage/Checkout/controllers/OnepageController.php, comment out this line (at or about line 227):
    // $session->clear();
  3. Now you are ready to implement your code, and you can refresh the order confirmation page as many times as needed, and the content will display.

Displaying Order Details on the Order Confirmation Page

// First, let's load some Load order details
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);

Displaying Shipping Address and Shipping Method

// Get shipping method
$shipping_method = $order_details->_data["shipping_description"];

// Get ship-to address information
$shipping_address_data = $order_details->getShippingAddress();

// Output the ship-to address information
echo "
" . $shipping_address_data['firstname']; echo " " . $shipping_address_data['lastname']; echo "
" . $shipping_address_data['street']; echo "
" . $shipping_address_data['city']; echo ", " . $shipping_address_data['region']; echo " " . $shipping_address_data['postcode']; echo "
" . $shipping_address_data['country_id'];

Displaying Products

/*** Product Listing ***/
// Iterate through each product listing the sku, the name, the quantity ordered, and the item price
foreach ($order_details->getAllItems() as $item) {
     echo '
'; echo '

Product:' . $item->getName() . '

'; echo '

' . Mage::helper("core")->currency($item->getPrice()) . '

'; echo '

' . round($item->getQtyOrdered(), 0) . '

'; echo '

' . Mage::helper("core")->currency($item->getPrice() * $item->getQtyOrdered()) . '

'; echo '
'; echo '

SKU: ' . $item->getSku() . '

'; echo '
'; } // Get the product id from the item (useful later for loading product attribute information) $product_id = $item->getData('product_id');

Displaying Payment Method and Billing Address

// Get payment information
$payment_method = $order_details->getPayment()->getMethodInstance()->getTitle();

// Get billing address information
$billing_address_data   = $order_details->getBillingAddress();

// Output the billing address information
echo "
" . $billing_address_data['firstname']; echo " " . $billing_address_data['lastname']; echo "
" . $billing_address_data['street']; echo "
" . $billing_address_data['city']; echo ", " . $billing_address_data['region']; echo " " . $billing_address_data['postcode']; echo "
" . $billing_address_data['country_id'];

Displaying Order Summary Amounts

/*** Billing amounts ***/
// These leverage the Magento core helper to format the numbers to currency
echo '

Subtotal:' . Mage::helper("core")->currency($order_details->subtotal) . "

"; echo '

Tax:' . Mage::helper("core")->currency($order_details->tax_amount) . "

"; echo '

Discount:' . Mage::helper("core")->currency($order_details->discount_amount) . "

"; echo '";

Loading custom product attribute information

// Load the CMS helper
$_cmsHelper = Mage::helper('cms');
$_process = $_cmsHelper->getBlockTemplateProcessor();
// Load the product model class
$product = Mage::getModel('catalog/product');
// Load the product into the class
// Uses ID we loaded earlier - see block about looping through products
$product->load($product_id);
// Load the learn block into the variable so we can test the size before displaying
// NOTE: "learn" is the identifier of the attributed.  use the id you defined for the attribute
$block = $product->getData('learn');
if (strlen($block) > 20) {
     echo '

Learn more about your new product

'; echo '
'; // Run the block through the cms filter to get cms code rendered properly echo $_process->filter($block); echo '
'; }

How to load a custom static CMS block into the order confirmation page directly

// Loads the CMS static block directly
// Note: the identifier for the cms block in this example is success_faq - adjust to suite your cms
echo $this->getLayout()->createBlock('cms/block')->setBlockId('success_faq')->toHtml();	   

2 Responses to Customizing and Styling Magento Order Confirmation

  1. Virgil says:

    Thanks for sharing with us this solution, it outputs all the information that I need.

    I am using this solution to send transaction information to a software similar to GA which uses JS to send the info

    I was able to implement everything but sending item info about each product .

    This is an example of the template I have to populate:

    …………………..
    },
    items: [
    {
    product_id: ‘PRODUCT-1’,
    unit_price: ‘10.00’,
    quantity: ‘1’
    },{
    product_id: ‘PRODUCT-2’,
    unit_price: ‘20.00’,
    quantity: ‘3’
    }
    ],
    discounts: [
    {
    name: ‘DISCOUNT 1’,
    amount: ‘5.00’
    },{
    name: ‘DISCOUNT 2’,
    amount: ‘10.00’
    }
    ]
    };

    any idea how I can populate the items and the discount section?

  2. Jake says:

    This is perfect. I’m so glad you wrote this as I was not sure how to get all this running. You had almost everything I needed. The one exception is shipping cost. Is there a way to get that?

Leave a Reply

(required)

comment on this post »

2 comments on this post

Virgil:  “Thanks for sharing with us this solution, it outputs all the information that I need. I am using this solution to send transaction information to a software similar to GA which uses JS to send the info I was able to implement everything but sending item info about each product . This is an example of the template I have to populate: ....................... }, items: [ { product_id: 'PRODUCT-1', unit_price: '10.00', quantity: '1' },{ product_id: 'PRODUCT-2', unit_price: '20.00', quantity: '3' } ], discounts: [ { name: 'DISCOUNT 1', amount: '5.00' },{ name: 'DISCOUNT 2', amount: '10.00' } ] }; any idea how I can populate the items and the discount section?”July 3, 2013 | 2:47 am
Jake:  “This is perfect. I'm so glad you wrote this as I was not sure how to get all this running. You had almost everything I needed. The one exception is shipping cost. Is there a way to get that?”July 9, 2013 | 8:40 pm