1

I'm using Commerce with the Commerce E-mail module to customize the e-mail sent to admins whenever there is an order (a payment is received) at the store. Payment is handled using PayPal WPS through the Commerce PayPal module.

On my site, buyers must input their e-mail addresses before moving on to PayPal, but these e-mail addresses (on my site) sometimes differ from the e-mail addresses registered on buyers' PayPal accounts.

For this reason, I want the admin e-mail (sent when an order is paid for) to include both the e-mail address that the buyer input on my Drupal site as well as their PayPal e-mail address.

Commerce E-mail provides a [commerce-email:order-items] token, but there is no token for the PayPal WPS information. This information is stored within Drupal; in the Order Admin UI, if you click on the payment details of the transaction, under Payload there is a printout of an Array that includes [payer_email] => thepayeremail@example.com.

Payload array:

Array (
    [13775355555-ipn] => Array
        (
            [mc_gross] => 17.00
            [invoice] => 555-1377531555
            [protection_eligibility] => Eligible
            [payer_email] => thepayeremail@example.com

How can I add the buyer's PayPal registered e-mail address ([payer_email]) into the admin e-mail sent by Commerce? It would be great if the solution would also allow use of other information in the payload array, such as the buyer's physical address.

1
+100

You can extract those array and add them data object. After adding them to the token replace function you can use that to create custom tokens which will help you to get the tokens. That custom tokens can now be used to get the buyer's email address

// Write a code for porting data to token_replace()
$data = new val();
$data->mc_gross = variable_get('mc_gross', '');
$data->invoice = variable_get('invoice', '');
$data->protection_eligibility = variable_get('protection_eligibility', '');
$data->invoice = variable_get('payer_email', '');
token_replace(['payload']', (array) $data);

--------write this in module_name.tokens.inc file------------------------------------------

 function hook_token_info() {
      $info = array();

      // Add any new tokens.
      $info['tokens']['payload']['mc_gross'] = t('Gross');
      $info['tokens']['payload']['invoice'] = t('Invoice');
      $info['tokens']['payload']['protection_eligibility'] = t('Protection Eligibility');
      $info['tokens']['payload']['payer_email'] = t('Email');
      // Return them.
      return $info;
    }


    function hook_tokens($type, $tokens, array $data = array(), array $options = array()) {
     $replacements = array();


    if ($type == 'payload') {
    foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'mc_gross':
              $replacements[$original] = $data['mc_gross'] ;
              break;
            case 'invoice':
              $replacements[$original] = $data['invoice'];
              break;
            case 'protection_eligibility':
              $replacements[$original] =  $data['protection_eligibility'];
              break;
            case 'payer_email':
              $replacements[$original] = $data['payer_email'];
              break;
          }
        }
      }

      return $replacements;
    }
  • Thanks for this, but where should I place the first part of the code (// Write a code for porting data to token_replace())? – Patrick Kenny Mar 22 '15 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.