Tags/topics: 
4
Answers
Vote up!
0
Vote down!

Add tokens to the commerce-order token types for commerce-order-total field components base-price shipping etc

Hi,

May be shortterm solution thinking for https://drupal.org/node/2063023
Adding simple tokens based on: http://www.andypangus.com/drupal-7-making-your-own-tokens

I made this custom module but I do not see the tokens showing up in the tokenlist. What do I do wrong please?

Module file:

<?php
/**
 * Implements hook_token_info().
 */
function Commerce_Order_Breakup_token_info() {

 
// Tokens for commerce order totals field.
 
$order = array();

 
$order['base-price'] = array(
   
'name' => t('Order total:Base price'),
   
'description' => t('The sub-total excluding shipping tax end other fees and charges.'),
  );
 
$order['shipping'] = array(
   
'name' => t('Order total:Shipping'),
   
'description' => t('The total cost of shipping.'),
  );
 
   
$order['tax_rate'] = array(
   
'name' => t('Order total:Tax'),
   
'description' => t('The Tax of the order including shipping.'),
  );
  return array(
   
'tokens' => array('commerce-order' => $order),
  );
}



/**
 * Implements hook_tokens().
 */
function Commerce_Order_Breakup_tokens($type, $tokens, array $data = array(), array $options = array()) {

 
$replacements = array();

  if (
$type == 'commerce-order' && !empty($data['commerce-order'])) {
   
$order = $data['commerce-order'];

    foreach (
$tokens as $name => $original) {
      switch (
$name) {
       
// replace commerce order total components tokens with actual values
        // from the order.
     
case 'base-price':
       
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'base_price');
      
// kpr($price_components);
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;

        case
'shipping':
         
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland');
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;
         
         
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;
         
        case
'tax_rate':
         
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'btw_hoog');
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;

      }
    }
  }

          return
$replacements;
}
?>

Info file:

name = Commerce Order Breakup tokens
description = A module with the Commerce Order Tokens: Base_price, shipping services and tax rates
package = Commerce Contributed
core = 7.x
dependencies[] = token
files[] = Commerce_Order_Breakup_tokens.module

How can I use these tokens now within my Commerce customer emails?

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

Asked by: Summit
on August 14, 2013

Comments

If you put all the code in a file MYMODULE.tokens.inc. The token module picks up these files automatically.

Your MYMODULE.module file just needs a comment but it is basically empty as follows:

<?php
/**
 * @file MYMDOULE.module
 * TODO: Enter file description here.
 */

Never end a file with a closing
?>

Also the case shipping try the following as you are only allowed one break the 2nd will never happen:

<?php
          $price_components0
= commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland');
         
$price_components1 = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');
         
$price_components    commerce_price_components_combine($price_components0, $price_components1);
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;


Never end a file with a closing
?>

Remove all closing ?>

In the info file no need for:

files[] = Commerce_Order_Breakup_tokens.module

- sohotech on August 15, 2013

4 Answers

Vote up!
0
Vote down!

Hi Sohotech, thanks for your comments. Did all this and tokens are showing now.
But using the tokens within a email I got following errors:

tice: Undefined index: data in commerce_price_components_combine() (line 1032 of sites//all/modules/commerce/modules/price/commerce_price.module). Backtrace:
commerce_price_components_combine(Array, Array) Commerce_Order_Breakup.tokens.inc:53
commerce_order_breakup_tokens('commerce-order', Array, Array, Array)

Warning: Invalid argument supplied for foreach() in commerce_price_components_combine() (line 1032 of sites/all/modules/commerce/modules/price/commerce_price.module). Backtrace:
commerce_price_components_combine(Array, Array) Commerce_Order_Breakup.tokens.inc:53

Notice: Undefined index: 0 in commerce_order_breakup_tokens() (line 54 of sites/all/modules/commerce/contributions/commerce_order_breakup_tokens/Commerce_Order_Breakup.tokens.inc). Backtrace:
commerce_order_breakup_tokens('commerce-order', Array, Array, Array)

Could you or someone else help me out here please?
Thanks a lot in advance!
Greetings, Martijn

Answer by: Summit
Posted: Aug 16, 2013

Comments

Hi,
I think it has to do with this peace of code:

<?php
case 'shipping':
         
$price_components0 = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland');
         
$price_components1 = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');
         
$price_components  commerce_price_components_combine($price_components0, $price_components1);
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;
?>

Greetings, Martijn
- Summit on August 16, 2013
Vote up!
0
Vote down!

Hi,

Looks like the commerce_price_component_combine only check one price array not both.

Try out a the following which allows for adding additional shipping rates into the token.

<?php
case 'shipping':
         
$price_components = array();
          if(empty(
$price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland'); }
          if(empty(
$price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland'); }

               
// Following is a dummy line to slot another shipping rate above
                // this comment. We never use the following line as at least one
                // shipping rate always gets set above. Just copy and change.
               
if(empty($price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_dummy'); }

                 
// Here the shipping price component has been set and
                 // the token gets replaced.
         
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
          break;
?>
Answer by: sohotech
Posted: Aug 20, 2013
Vote up!
0
Vote down!

Had a rethink on the above and I think the following reads a little better:

<?php
           
case 'shipping':
                   
$price_components = array();
                  if (empty(
$price_components['0'])) {
                   
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland');
                   
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');
                  }
                       
// Following is a dummy line to slot in another shipping rate above
                        // this comment. We never use the following line as at least one
                        // shipping rate always gets set above. Just copy and change.
                        //  if(empty($price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_dummy'); }

                  
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);

                  break;
?>
Answer by: sohotech
Posted: Aug 21, 2013
Vote up!
0
Vote down!

Oops the above is definitely wrong Should have been as follows:

<?php
           
case 'shipping':
                   
$price_components = array();
                  if (empty(
$price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland'); }
                  if (empty(
$price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');  }

                       
// Following is a dummy line to slot in another shipping rate above
                        // this comment. We never use the following line as at least one
                        // shipping rate always gets set above. Just copy and change.
                        //  if(empty($price_components['0'])) { $price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_dummy'); }


                  
$replacements[$original] =  commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);

                  break;
?>
Answer by: sohotech
Posted: Aug 21, 2013