grouping packages in hostpay

By default, it’s not obvious how to grouping packages in hostpay – packages in hostpay are stored separately, i.e if you have a “hosting package” and then set multiple subscription periods, i.e monthly, yearly etc. then the default hostpay template will output them all as individual products.

We actually want to display them as packages, but with options. Almost like a product with a variation in colour or size, but in this case, its the billing cycle.

so, here’s my code to achieve this (every line of code in brackets i know, i’m learning, and hostpay is not the best place for code writing skills to learn from):

[code]
<% product_names = Array.new %>
<% name_to_products = Hash.new %>
<% hosting_products.each do | product | %>
<% if (not name_to_products[product.name]) %>
<% product_names.push(product.name) %>
<% name_to_products[product.name] = Array.new %>
<% end %>
<% name_to_products[product.name].push(product) %>
<% end %>

<table border="1">
<thead>
<tr>
<% product_names.each do | pname | %>
<th><%= pname %></th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<% i = 0 %>
<% product_names.each do |pname| %>
<td>
<h4><%= pname %></h4>
<p><%= name_to_products[pname][0].description %></p>
<% name_to_products[pname].each do |product| %>
<input type=’hidden’ name=’base_renewal_period-<%= i %>’ value="<%= product.base_renewal_period %>" />
<input type=’hidden’ name=’quoted_unit_price-<%= i %>’ value="<%= billing.price(product.unit_price) %>" />
<input type=’radio’ name=’add-product’ value='<%= product.specific_type.html %>:<%= i %>:base_renewal_period,quoted_unit_price,domain’ /><%= billing.currency_symbol.html %><%= billing.price(product.unit_price, :use_tax_prefs) %>
<% if product.base_renewal_period != ‘never’ %>
per <%= product.unit_period.to_s_short.html %>
<% end %>
<% if product.setup_fee != 0 %>
plus a one off fee of <%= billing.currency_symbol.html %><%= sprintf("%.2f", if billing.include_tax? then billing.price(product.setup_fee) else product.setup_fee end) %>
<% end %>
<% if product.has_free_products? %>
<br />
includes free domains
<% product.free_products_list.each do | free_product | %>
<%= free_product["free_name"] %>
<% end %>
<% end %>
<% i = i + 1 %>
<% end %>
</td>
<% end %>
</tr>
</tbody>
</table>
[/code]

this post will be turned into a tutorial for my own benefit eventually, and i will be adding notes and code as i go along too.




2 Comments

I’m surprised there isn’t 10,000 comments to this I’ve just spent 15 hours trying to get this to work as you have!

Saved me so much time!

I’ll study the code so I can adapt to it but seriously,

Thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *