3

Is there any way to get programmatically the selected (by customer) payment method from the $order_wrapper variable in Drupal Commerce?

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
3

I don't think there's anything built in, but you can use an EntityFieldQuery to grab the transaction(s) related to the order, and from there the payment type(s) used:

$query = new \EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_payment_transaction')
  ->propertyCondition('order_id', $order->order_id);

$results = $query->execute();
if (!empty($results['commerce_payment_transaction'])) {
  $transactions = entity_load('commerce_payment_transaction', array_keys($results['commerce_payment_transaction']));
  foreach ($transactions as $transaction) {
    $payment_method = $transaction->payment_method;
  }
}
0
3

Each payment method is a rule. Let's get the rule name than:

$payment_method = '';
$order = commerce_order_load($order_id);
$r = explode('|', $order->data['payment_method']);
if (count($r) > 1){
  $rule = rules_config_load($r[1]);
  $payment_method = $rule->label;
}
1

Based on Clive's answer above here's a rules condition set up for order is payment method.

Replace custom_commerce with the name of your module.

// custom_commerce.rules.inc

/**
 * Implements hook_rules_condition_info().
 */
function custom_commerce_rules_condition_info() {
  $conditions = array();

  // We have two or more payment methods we sometimes need to distinguish
  // between them.
  $conditions['custom_commerce_order_is_payment_method'] = array(
    'label' => t('Order is Payment Method'),
    'parameter' => array(
      'commerce_order' => array(
        'type' => 'commerce_order',
        'label' => t('Order'),
      ),
      'commerce_payment_method' =>array(
        'type' => 'text',
        'label' => t('Payment Method'),
        'options list' => 'custom_commerce_payment_method_option_list',
      ),
    ),
    'group' => t('Custom Commerce'),
    'callbacks' => array(
      'execute' => 'custom_commerce_order_is_payment_method',
    ),
  );

  return $conditions;
}

/**
 * @param stdClass $order
 * @param string $expected_payment_method
 * @return bool
 */
function custom_commerce_order_is_payment_method($order, $expected_payment_method) {
  $query = new \EntityFieldQuery();
  $query->entityCondition('entity_type', 'commerce_payment_transaction')
    ->propertyCondition('order_id', $order->order_id);

  $results = $query->execute();
  if (!empty($results['commerce_payment_transaction'])) {
    $transactions = entity_load('commerce_payment_transaction', array_keys($results['commerce_payment_transaction']));
    $transaction = end($transactions);
    $payment_method = $transaction->payment_method;
    return $payment_method === $expected_payment_method;
  }
  return FALSE;
}

/**
 * Returns key value pairs for the rules parameter drop down.
 */
function custom_commerce_payment_method_option_list() {
  $payment_methods = commerce_payment_methods();

  return array_map(function($v) {
    return $v['title'];
  }, $payment_methods);
}

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.