commerce-email-order-items should be views based. That would enable admins to alter the view in the mails. This would solve a lot of feature requests that were made here.

CommentFileSizeAuthor
#16 htmlmail.tpl_.php_.txt3.46 KBlmeurs

Comments

johnpitcairn’s picture

Hmm ... see also #1332698: Allow theming of [commerce-order:commerce-email-order-items].

Occurs to me you could use that and call views_embed_view() within the theme override as an interim solution.

johnpitcairn’s picture

If the order id were passed in, which it isn't. Rats.

johnpitcairn’s picture

@elmotri, this is a really good idea. Are you able to put an appropriate default view together? It would need to handle everything that commerce_email_order_items() does.

elmotri’s picture

The view should be the same as the "commerce_cart_summary" view. I tested this and it worked. I had to set the permission "View own orders of any type". Here is the code:

function commerce_email_order_items($order, $theme=TRUE) {
  $view = views_get_view('commerce_cart_summary');
  return $view->preview('default', array($order->order_id));
}

I think there should be a separate view for the mail but you can simply copy the commerce_cart_summery view as the default view for email. Maybe some theming is needed.

johnpitcairn’s picture

Thanks for that.

I think we should integrate this with the patch at #1332698: Allow theming of [commerce-order:commerce-email-order-items]. We need to ensure commerce_email_order_items() still returns the old table array if $theme == FALSE, and I think the view should be called from within the new theme function in that patch. If somebody wants to use a different view, or the old table code, or something else entirely, they can override the theme function.

OK to close this issue and continue in #1332698: Allow theming of [commerce-order:commerce-email-order-items] ?

johnpitcairn’s picture

Hmm. Yes, we'd need to add a default view, and theme the view to add the appropriate inline styles, exactly replicating the current themed table output. That's more work than I have time for.

But, assuming the patch at #1332698: Allow theming of [commerce-order:commerce-email-order-items] gets committed, there's now nothing stopping anyone from overriding theme_commerce_email_order_items() to use a view, and also overriding the view theme function or .tpl file to add appropriate inline styles for html email.

summit’s picture

Hi,

Does this https://drupal.org/node/1605586#comment-6240576 work with latest 2.x dev?
@Elmotri could you please explain how to use views as template for the commerce-email order notification (admin/client).

Thanks a lot in advance for your reply!
Greetings, Martijn

johnpitcairn’s picture

Update: the patch from #1332698: Allow theming of [commerce-order:commerce-email-order-items] has been commiteed to latest dev, so you can override that theme function in custom module code or in template.php to alter the default table, replace it with a view, or do whatever you want, without hacking the module.

Note that the problem with using a view, or making this views-based by default, is that Views does not support inline style markup, which is essential for good html email. There would need to be additional manipulation of the view theme output to add inline styles - therefore admins would not be able to just use the Views UI to change things - they'd also need to alter the theme output.

This issue should probably be closed as "won't fix" for that reason.

lmeurs’s picture

@Summit: By writing a custom theme function you can easily generate views tables. By using Emogrifier (Drupal Module) it's possible to convert stylesheet rules to inline style attributes.

function mytheme_commerce_email_order_items($variables) {
  // Get order wrapper.
  $order_wrapper = $variables['commerce_order_wrapper'];

  // Get view.
  $view = views_get_view('commerce_cart_summary');

  // Preview the given display, with the given arguments.
  $view_preview = $view->preview('default', array($order_wrapper->getIdentifier()));

  return $view_preview;
}

Thanks all!

EDIT: Or use views_embed_view() which is shorter and more secure since it also checks permissions etc.

function mytheme_commerce_email_order_items($variables) {

  // Get order wrapper.
  $order_wrapper = $variables['commerce_order_wrapper'];

  // Get rendered view.
  return views_embed_view('commerce_cart_summary', 'default', $order_wrapper->getIdentifier());
}

Since the default display also outputs contextual links, we added a custom display to the "Shopping cart summary (Commerce Order)" view.

summit’s picture

Hi @lmeurs,

I altered my commerce_cart_summary view to have shipping costs nicely under subtotal and to have it correctly shown for dutch customers.
These alterations are done by:

function commerce_checkout_order_commerce_price_formatted_components_alter(&$components, $item, $entity) {
  if (isset($components['base_price'])) {
    foreach ($components as $component_name => $component) {
      if (strpos($component_name, 'flat_rate_') === 0) {
        $components[$component_name]['weight'] = $components['base_price']['weight'] + 1;
      }
    }
  }
}

Will this alteration in the view also be visible in the email please?

Thanks a lot in advance for your reply!
Greetings, Martijn

lmeurs’s picture

@summit: It will if your module's name is commerce_checkout_order. To be sure I tested the hook with my module's name and it was invoked on the cart and order preview pages, so the alterarions done by the hook will also be output in the e-mails when using a view.

summit’s picture

Hi @lmeurs,

Thanks a lot for helping me so much. Very much appreciated!! I have still two errors using this method, which may have to do with registration of the function?, see: theme not registered: https://drupal.org/node/1406314#comment-5474364

Errors:

Theme hook htmlmail not found.
The theme('htmlmail', $message) function did not return any text. Please check your template file for errors.

What should be the function call to registrate mytheme_commerce_email_order_items if this off course helps with the errors?

Greetings, Martijn

summit’s picture

Hi, #12 was not really related. The Errors where gone with correct setting the htmlmail tpl.php files into the template subfolder of the theme.

https://drupal.org/node/1605586#comment-7875911 #9 working great. Would love to have commerce_email using views!
One add on if possible. Also place Address in the view so that information can also be gathered through this view.

Thanks for considering!
greetings, Martijn

waltercat’s picture

@lmeurs Can you break down a little bit more as to what files need to be created and edited in order to generate a view in the email? I tried adding this code to my htmlmail--variable_email--commerce_email_admin_order.tpl.php file but nothing happened:

<?php
function mytheme_commerce_email_order_items($variables) {

  // Get order wrapper.
  $order_wrapper = $variables['commerce_order_wrapper'];

  // Get rendered view.
  return views_embed_view('commerce_cart_summary', 'default', $order_wrapper->getIdentifier());
}
?>

Not really sure if this can be directly added to the tpl or if it is supposed to be a module or something. If you could further clarify, that would be great!

summit’s picture

Hi,
this code should be added to your TEMPLATE.PHP from your theme!
Then you can style your email through htmlmail.tpl.php
@lmeurs; Laurens, thanks a lot for bringing this into commerce_email. It is great to use views for this!

Greetings,
Martijn

lmeurs’s picture

StatusFileSize
new3.46 KB

@waltercat: Like summit says. Put the mytheme_commerce_email_order_items() hook inside your template.php file, change mytheme to the name of your theme and clear your caches.

Your htmlmail.tpl.php file should contain all the CSS (see attachment, I had to rename it to avoid drupal.org security warnings), also for the views table, and I invoke Emogrifier from there to generate inline styling. I know this should not be done from inside a template file, but I do not know which hook to invoke to alter the output of a template. Make sure the path to your Emogrifier library is correct and you do not need the Emogrifier Drupal module for this, just the Emogrifier library.

Then, when calling views_embed_view() on the default display as an admin, the view will get an 'Edit view' link prefixed. By adding ie. a block display named 'mail' to the commerce_cart_summary view and calling this display, the edit link will not be shown:

return views_embed_view('commerce_cart_summary', 'mail', $order_wrapper->getIdentifier());

@summit: You are welcome, it was nice meeting you!

summit’s picture

Hi,

This is not working for this token using commerce_message to email an invoice to the customer.
I would very much like to send the commerce_cart_summary mail display in this email, but it keeps taking the original commerce_email table instead of the view.
Using the same token: [message:message-commerce-order:commerce-email-order-items]

It is working great using commerce_message order email, but it is not working using commerce_message custom invoice email.
Somehow the theme override is not triggered I think...

Greetings, Martijn

summit’s picture

I tried all weekend to get this working, but theme override no go.
With completely changing the function it worked: https://drupal.org/node/1605586#comment-6240576
Now everywhere where I call [message:message-commerce-order:commerce-email-order-items] the correct view is shown! Yoohoo.

I plead for making this module completely depending on views because views is the standard way for drupal and with https://drupal.org/node/1605586#comment-7939397 htmlmail.tpl.php it is much easier to theme the email!

I disagree with the fact the default table should not be views. The user sees a view on the checkout page, and it is only better to show the exact same view in an email as confirmation!

Greetings, Martijn

johnpitcairn’s picture

It presumably doesn't work with message_order tokens because that's calling a different token chain. The commerce email token chain is [commerce-order:commerce-email-order-items].

Views should not be used by default because a view cannot apply inline css styling. You would still need to override a theme function or .tpl, or use an additional module to produce best-practice html email (with inline styles). For many users this would be difficult or impossible.

summit’s picture

Hi, I understand, but how to get to this token through another token chain?

function mytheme_commerce_email_order_items($variables) {

  // Get order wrapper.
  $order_wrapper = $variables['commerce_order_wrapper'];

  // Get rendered view.
  return views_embed_view('commerce_cart_summary', 'default', $order_wrapper->getIdentifier());
}

Is not working within that chain [message:message-commerce-order:commerce-email-order-items], and for other purposes, I need commerce_message.
The chain [message:message-commerce-order:commerce-email-order-items] comes back to the commerce-email-order-items from this module..

Thanks a lot for your reply in advance!
Greetings, Martijn

johnpitcairn’s picture

I'd have to take a look at how message_order implements its token, but I can't right now. Have you tried the message_order issue queue?

(PS: I am not the commerce_email maintainer)

summit’s picture

Hi,
I made these issues within Commerce Message issue queue: https://drupal.org/node/2109863 (because my problem is with the invoice email) and https://drupal.org/node/2110431 (because I would very much like the token chain [message:message-commerce-order:commerce-email-order-items] be themable).

Right now the only working solution for me is hacking commerce_email and changing function commerce_email_order_items like in https://drupal.org/node/1605586#comment-6240576

Greetings, Martijn

lanceh1412’s picture

Thanks. #16 This works for me with 'default' but not 'mail'. I've added a block display called 'mail' but I no longer get the token substitution. Is there something else that needs to be done to get the block to display?

johnatan123’s picture

Issue summary: View changes

Hi can you help me please ? i used this..

<?php
function bobr_commerce_email_order_items($variables) {

 
// Get order wrapper.
  $order_wrapper = $variables['commerce_order_wrapper'];

 
// Get rendered view.
  return views_embed_view('clone_of_shopping_cart_summary', 'default', $order_wrapper->getIdentifier());
}
?>

then i themed it and it works great.. but only as an administrator.. when i do an order as an anonymous, the view table is not showing up in email..

any ideas how to fix it?

Thanks

pyxio’s picture

@johnatan123 hi, did you ever solve this? i have same exact problem :( please let me know. thanks!

lmeurs’s picture

@johnatan123 & @drupalstrap: Try temporarily giving anonymous users "View any Product product" permission at admin/people/permissions (ignore the permission's warning for now). If this solves your problem, see #1303194: Clarify security implications of granting "View any Product product" permission, especially my comment at #56 and rszrama's (developer at Commerce Guys) comment at #57.

pyxio’s picture

@johnatan (anybody else with the same problem) - i solved simply via user permissions ... under orders section enable anonymous users to view their own orders. seems simple but this not commerce default so you need to tick that box! Cheers

pyxio’s picture

@Imeurs - just saw your post! thanks ... however i think the one is view own order permissions. anyway, playing around with both will get the items to show. thanks so much for the awesome function! cheers kevin

jcherbert’s picture

@lanceh1412, I had the same problem as #23, I found that by going to export view the block definition was:
$handler = $view->new_display('block', 'mail', 'block_2');
then, using:
return views_embed_view('commerce_cart_summary', 'block_2', $order_wrapper->getIdentifier());
in template.php, this worked.

Tommy_001’s picture

I know this very late in the chain, but I just wanted to say thank you Imeurs, for the tips regarding how to theme the template and how to get rid of the contextual link. Thanks!

rszrama’s picture

Status: Active » Closed (outdated)