Print commerce pricing attributes to email receipt
Question
Users cannot see what size or color shirt they purchased in the email receipt because tokens won't print the attribute fields provided by Commerce Product Attributes / Commerce Pricing Attributes / Commerce Product Option combo.
- User selects shirt size: XL and color: Green
- Both fields can be previewed during checkout.
- Email receipt received doesn't show any attributes, user cannot check what size/color they ordered. (All this can be achieved using default CCK fields under product type, but we're using the combination mentioned above for multiple reasons, many products got different option sets to chose from and certain attributes selected adds extra price to total, like eg size: XXXL costs extra $3+ and this cannot be achieved with default fields).
One of the tokens I found is like eg. [commerce-order:commerce-line-items:0:commerce-pricing-attributes]
and email on checkout prints it like this:
Product: BK Test
a:1:{s:9:"coveralls";a:8:{s:25:"field_coverall_embroidery";N;s:25:"field_coverall_text_color";N;s:10:"field_font";N;s:16:"field_shirt_name";N;s:16:"field_patch_side";N;s:12:"field_length";s:1:"2";s:21:"field_coveralls_color";s:1:"4";s:19:"field_coverall_size";s:2:"16";}}
Also more tokens available at admin/help/token
like eg.: Attributes [commerce-line-item:commerce-product:field_attributes] Pricing Attributes field.
and combining them with [commerce-order:commerce-line-items:0:commerce-product]
won't work, already tried dozens of variations.
E.g. in Order's view page I addded the view Commerce Line Item: Product Attributes
and it displays correctly.
How to print this in the email receipt?
Solution
install views rules module, make a views-rules of orders, you will need OID as a contextual filter, add it as allways, then in middle column, in parameters settings set your OID as:integer, add all fields/attributes you wish, now in middle column settings, set your fields/attributes values as text, choose 'use rendered output'.
Then in your send-email-to-customer rule, add a views-rule with your new view, actions: fecth entity by id:ORDER with your variable OID, now you have all fields/attributes you added avilable as tokens, built your email body like: thanks [user:name], your order [OID] is in proccess, you bought : T-shirt [tshirt:value], T-shirt-size: [tshirt-size:value], ............. .
if you can not use HTML tags, here is an exported rules_action_info() rules code that i used in one site, adds a new action: build-email-body, it shows you how to built a row for a 'nice' html look alike. if you need this feature, you will need to make a custom module called: rulesbodyemail, change all $variables and code as needed, take a close look how is built variable: $text_formatted_body, and is how is returned.
<?php
/**
* Implement hook_rules_action_info()
* Declare any meta data about actions for Rules
*/
function rulesbodyemail_rules_action_info() {
$actions = array(
'buitl_email_body' => array(
'label' => t('build-email-body'),
'group' => t('cadena'),
'parameter' => array(
'node_id' => array(
'type' => 'integer',
'label' => t('node id'),
),
'parcialidad_title' => array(
'type' => 'text',
'label' => t('parcialidad_title'),
),
'parcialidad_monto' => array(
'type' => 'decimal',
'label' => t('parcialidad monto'),
),
'parcialidad_cliente' => array(
'type' => 'text',
'label' => t('cliente'),
),
'parcialidad_fecha' => array(
'type' => 'date',
'label' => t('parcialidad fecha'),
),
'parcialidad_aseguradora' => array(
'type' => 'text',
'label' => t('cia aseguradora'),
),
'parcialidad_ramo' => array(
'type' => 'text',
'label' => t('ramo'),
),
'parcialidad_cobertura' => array(
'type' => 'text',
'label' => t('cobertura'),
),
'parcialidad_fecha_formateada' => array(
'type' => 'text',
'label' => t('feha formateada'),
),
),
'provides' => array(
'text_formatted_body' => array(
'type' => 'text',
'label' => t('text_formatted body'),
'wrap' => TRUE,
//'format' => 'full_html'
),
),
),
);
return $actions;
} // fin de actions_info
// rules function procesar row
function buitl_email_body($node_id, $parcialidad_title, $parcialidad_monto,
$parcialidad_cliente, $parcialidad_fecha, $parcialidad_aseguradora,
$parcialidad_ramo, $parcialidad_cobertura, $parcialidad_fecha_formateada) {
// variable $hoy esta en UNIX
$hoy = time();
$formatted_hoy = date('j/M/y', $hoy);
//drupal_set_message($parcialidad_fecha_int . ' $parcialidad_fecha ', 'status', FALSE);
//drupal_set_message($formatted_fecha . ' $formatted_fecha ', 'status', FALSE);
//prepara las variables y calcula dias vencidos
$parcialidad_fecha_int = strip_tags($parcialidad_fecha);
$hoy_int = (int)$hoy;
$fecha_int = (int)$parcialidad_fecha_int;
$diff = $hoy_int - $fecha_int;
$diff = round($diff/86400);
// formato fecha y $ pesos
$monto_format = "$".number_format($parcialidad_monto, 2);
$formatted_fecha = date('j/M/y', $parcialidad_fecha_int);
//hace el render del row
$text_formatted_body =
"
▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
▲ PARCIALIDAD ▲ $parcialidad_cliente
Parcialidad: $parcialidad_title
Monto: $monto_format
Venció: $formatted_fecha; Días vencidos: $diff
Aseguradora : $parcialidad_aseguradora
Ramo: $parcialidad_ramo; Cobertura: $parcialidad_cobertura
" ;
//return $text_formatted_body
return array('text_formatted_body' => $text_formatted_body);
} // fin de funcion rules procesar row
it returns this as the email body:
image of rule to send emails:
image of body email value
OTHER TIPS
<?php
/**
* Implements hook_token_info().
*/
function mymodule_token_info(){
$info['tokens']['commerce-order']['line_item_detail'] = array(
'name' => t('Line Item Product Detail List'),
'description' => t('List all the Product Line Item Detail of the order')
);
return $info;
}
/**
* Implements hook_tokens().
*/
function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if($type == 'commerce-order'){
$order = $data['commerce-order'];
$order_wrapper = entity_metadata_wrapper('commerce_order',$order);
$lineItems = $order_wrapper->commerce_line_items->value();
$lineItemArray = array();
foreach($lineItems as $key => $value){
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item',$value);
if($line_item_wrapper->type->value()=='product'){
$product_title = $line_item_wrapper->commerce_product->title->value();
$quantity = $line_item_wrapper->quantity->value();
$lineItemArray[] = $product_title.' x '.$quantity;
}
}
foreach ($tokens as $name => $original) {
switch ($name) {
case 'line_item_detail':
$variables = array(
'items' => $lineItemArray,
'type' => 'ul',
);
$replacements[$original] = theme('item_list', $variables);
break;
}
}
}
return $replacements;
}
This is the sample code.
You can modify the result you want at hook_tokens().
I hope this will help you.