1

I want award users after completing the checkout process. For this task I use this modules (Commerce, UserPoints, Commerce Credits).

I create a product with a price equal 15$ and create a rule which award user after complete checkout. When checkout is complete, rule works great, but it gives user 1500 userpoints instead 15. I tried use module (Commerce Price Decimals Formatter, Commerce Extra Price Formatters) which remove decimals from commerce also I tried use some code from internet but all of it doesn't solve my issue.

Does anybody know solution for this issue?

it's code that I found on the internet:

<?php
/**
 * Implements hook_commerce_currency_info_alter().
 */
function Zero_commerce_currency_info_alter(&$currencies, $langcode) {
  $currencies['USD']['format_callback'] = 'Zero_rub_currency_format';
}

 /**
 * Currency format callback.
 */
function Zero_rub_currency_format($amount, $currency, $object) {
  $round_amount = commerce_currency_round(abs($amount), $currency);
  $price = number_format($round_amount, 0, $currency['decimal_separator'], $currency['thousands_separator']);

  $replacements = array(
    '@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
    '@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
    '@price' => $price,
    '@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
    '@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
    '@negative' => $amount < 0 ? '-' : '',
    '@symbol_spacer' => $currency['symbol_spacer'],
    '@code_spacer' => $currency['code_spacer'],
  );

  return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
}

It is my Rule:

{ "rules_grantpointaftercheckout" : {
"LABEL" : "GrantPointAfterCheckOut",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "userpoints_rules", "commerce_checkout" ],
"ON" : { "commerce_checkout_complete" : [] },
"DO" : [
  { "data_convert" : {
      "USING" : {
        "type" : "integer",
        "value" : [ "commerce-order:commerce-order-total:amount" ],
        "rounding_behavior" : "down"
      },
      "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
    }
  },
  { "userpoints_action_grant_points" : {
      "user" : [ "site:current-user" ],
      "points" : [ "conversion-result" ],
      "tid" : "0",
      "entity" : [ "site:current-cart-order" ],
      "operation" : "bla bla bla",
      "display" : "1",
      "moderate" : "approved"
    }
  }
]
}
}
  • Merci for the accept! I'd be interested to see your own (additional) answer including an updated version of your rule, which shows what I suggested in my answer. If you do: +1 from me ... please???? – Pierre.Vriens Feb 18 '17 at 19:14
1

Have a look at the Drupal Commerce issue about "Revise the way we handle price amount values". Especially comment nr 37 in it, which states:

The price field is and will continue to store price amounts as integers ...

So rounding an amount of 1500 dollarcents is ... still 1500, right? And that is the amount of user points you're granting. And because of that, your rule works as implemented: you're using that value of 1500 as the amount of userpoints to be granted.

Obviously, all you need to do next is to expand your rule to devide the price by 100 first (before rounding it). I assume you know how to do that, if not use this link ... bonus hint: have a look at the example rule included in the answer to "How to add up all items of a field collection field using Rules?", especially the "Calculate a value" Rules Action.

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.