During payment procession I want to change transaction statuses several times. But I can load ttransaction only by its ID not by order ID.
But during payment period I can mostly operate with order_id.
There should be function which loads transaction by order_id.
Thanks.

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

I don't think we'll be making this change, as it's possible for there to be multiple payment transactions per order ID. There's already a core Drupal API feature called the EntityFieldQuery that would do the exact same thing, load an entity based on some property (i.e. load payment transactions based on their order ID property). I recommend you research that and use that to load your payment transactions.

a.ross’s picture

Here's an example of how to do that:

  $query = new EntityFieldQuery;
  $result = $query
    ->entityCondition('entity_type', 'commerce_payment_transaction')
    ->propertyCondition('order_id', $order->order_id)
    ->propertyOrderBy('transaction_id', 'DESC')
    ->range(0, 1)
    ->execute();

Docs for EFQ are here: http://api.drupal.org/api/drupal/includes!entity.inc/class/EntityFieldQu...

cafuego’s picture

Issue summary: View changes

For the record, you can use the commerce API to load the entire payment object (or objects in case of multiple transactions) pretty easily:

  // Assuming you have an $order object you want payment info for.
  $payments = commerce_payment_transaction_load_multiple(array(), array('order_id' =>  $order->order_id));

  // If you just have one and that's all you want...
  $payment = !empty($payments) ? array_shift($payments) : NULL;
Samba B’s picture

@cafuego Thanks! This saved my day! :)

tostinni’s picture

I found this answer too but it's important to notice that commerce_payment_transaction_load_multiple() can also load unsuccessful attempt of the same transaction.

So in order to retrieve only the correct object you have to make :

  // Assuming you have an $order object you want payment info for.
  // Limit to successful payments.
  $payments = commerce_payment_transaction_load_multiple(array(), array('order_id' =>  $order->order_id, 'status' => 'success'));

  // If you just have one and that's all you want...
  $payment = !empty($payments) ? array_shift($payments) : NULL;