small medium large xlarge

Generic-user-small
02 Feb 2011, 14:50
Stuart J Nettleton (7 posts)

Hello, There are many attributes in each Order line_item, as can be seen in using rails console with say Order.find(1).line_items. Yet the confirmation email output from <%= render @order.line_items %> so limited? I would like to show more information in the confirmation email, for example an analogue of the cart. For example, with product price recorded in each line item, how could this be shown it in the confirmation email? Any thoughts would be appreciated. Thanks, Stuart

Zim_pragsmall
04 Feb 2011, 22:40
Augusto Rodriguez (9 posts)

Hi Stuart,

It took me a bit to understand what you meant, but I think this is the solution.

This template will display the quantity, title of product and total line price.

replace app/views/line_items/_line_item_text.erb with

<%= sprintf("%2d x %-50s %s",
            line_item.quantity,
            truncate(line_item.product.title, :length => 50),
            number_to_currency(line_item.total_price)) %>

you’ll need this method to order.rb calculate the total of the cart

add to order.rb

class Order < ActiveRecord::Base
  [...]
  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end
  [...]
end

replace order_received.text.erb with

Dear <%= @order.name %>
Thank you for your recent order from The Pragmatic Store.

You ordered the following items: 
<%= render @order.line_items %> 
--------------------------------------------------------------
     Total                                              <%= number_to_currency(@order.total_price) %>

We'll send you a separate e-mail when your order ships.

And this is the email I got

Subject: Pragmatic Store Order Confirmation 

Dear augusto
Thank you for your recent order from The Pragmatic Store.

You ordered the following items: 
 1 x Programming Ruby 1.9                               £49,50
 1 x Web Design for Developers                          £42,95
 
--------------------------------------------------------------
     Total                                              £92,45

We'll send you a separate e-mail when your order ships.

The formatting is not great (I hate the magic spaces in the email template), but it’s just an idea of how it should look. The magic function is sprintf, which has a trillion parameters, the good thing is that it behaves in the same was as in c, c++, java and many other languages.

Generic-user-small
06 Feb 2011, 23:59
Stuart J Nettleton (7 posts)

Hi Augusto, Your solution in modifying the template is much appreciated. I had come up with a ruby calculation but your solution is much more elegant. Many thanks, Stuart

You must be logged in to comment