Skip to content

Advanced email content

Jacob edited this page Sep 18, 2015 · 1 revision

Advanced email content

This section should give a quick overview on how to implement more advanced email content. By default the email editor will give an easy option of inserting text and pictures, but when it comes to more personalised data you need to do some programming. If you have a basic understanding of PHP and HTML then this should not be to hard.

Step 1 - Include custom template files (*.phtml)

In your email content you need to tell the email composer that you want to insert a template to your email at any given place. To do that you need to add the following Magento syntax to your email, best is to do that in the code viewer instead of the WYSIWYG editor.

{{block type="mzax_emarketing/template" template="mzax/email/cross-sells.phtml" order=$order}} (Hint: you can use auto-complete by typing mage.block)

This will tell the email composer to look for the template mzax/email/cross-sells.phtml in your theme folder. The type parameter will define the block class to use when rendering your template. In this case we can use the block class provided by the extension called mzax_emarketing/template which will have some useful methods, more on that later. Blocks are quite topic them self, and if you are not familiar with the layout part of Magento it might be worth reading up on it. There is heaps of articles and tutorials out there.

To make your template do something cool, it needs some data to work with. Now this can be a bit more confusing, as the data that is available depends the recipient provider you have selected for your email campaign. So for instance, if you use Magento Customers as email provider then you will not have any order information but instead only have a $customer object available, hence you can only pass the customer object to your template.

E.g. {{block type="mzax_emarketing/template" template="my_emails/last-viewed-products.phtml" customer=$customer}}

If you would use the Magento Order as email provider, then you will have the $order object, and maybe a $customer object (if not a guest-checkout) available. Likewise a shopping cart will have a $quote object available.

Let's assume you send out your emails to the people that have ordered last week using the Magento Order recipient provider. This will give you the $order object which you can pass to your template. Enter the code below to your email.

{{block type="mzax_emarketing/template" template="my_emails/related-products.phtml" order=$order}}

Step 2 - Create the template file

Now that you have added the above snipped to your email, you need to create the mentioned template file, in our case this is "./app/design/frontend/[package]/[theme]/mzax/email/cross-sells.phtml". Obviously you need to replace the [package] and [theme] with your Magento theme configuration.

In your template you can now add any text and html code, which will show up in your email. You can now use PHP code to access the $order object that you passed on to your template from the above step. All you need to do is use $this->getOrder() to retrieve it.

Step 3 - Add personalised content

If you have defined as block type the class mzax_emarketing/template, then you have access to some nice helper methods that can provide you more use full data. For instance:

  • $this->getLastOrders( $customer, $count = 6 ) This will return the last X orders for the specified customer as a sales order collection. You can add any filters to the class or change the order by altering the collection*.

  • $this->getAllItems( $object, $attributes = '*' ) Use an object, such as $order or $quote and retrieve all its child items including the product instance that can get retrieve via $item->getProduct(). This is what you need if you want to display for example all the products of a cart/quote or order object. Have a look at the (sample template)[] for the product order template.

  • $this->getCrosssellProducts( $object )

  • $this->getUpsellProducts( $object )

  • $this->getRelatedProducts( $object ) Will retrieve all up-/cross-sells-/related-products from the given object which can be a product by it self or an order/quote object to get a combination of all. You can for instance display any up-sell products that are available to the products that were bought buy the $order. So 3 days after shipping, you can send out an email to the customer saying “Thank you” and provide him with matching products and a coupon code.

  • $this->getLastViewedProducts( $customer, $count = 6, $lastDays = false ) Why only doing abandon cart emails if you could check for abandon products. This method will return the last viewed products by a given customer and return it as a product collection. Have a look at (this sample template)[].

  • $this->formatPrice( $price ) Will format the price in the right currency. When you retrieve the prices from your products (e.g. $product->getPrice()) they return a plain unformatted number, use this method to format the price according the setting of your store. ($this->formatPrice( $product->getPrice()))

  • $this->getProductImage( $product, $object, $attribute = 'small_image', $size = 100)) A simple method that will get you the product image path for the specified product.

You may want to look at this example and adjust it as you wish. Remember you can just copy past that template anywhere in your theme folder and set the correct path in your {{block ...}} directive.

Clone this wiki locally