0

I had created a custom module to update order state and payment state. In my controller I used the following code to change the order state of order.

 $order = \Drupal\commerce_order\Entity\Order::load($order_id);

 if ($payment_status === 'payment.captured'){
   $order->set('state', 'completed');
   $order->save();
 }
 else {
   $order->set('state', 'pending');
   $order->save();
 }

How can I now change the payment state of a payment which comes inside that order?

This is the screenshot of my payment. Now the payment state is pending. I want to change it to something else by using code, for example completed.

This is the screenshot of my payment. Now the payment state is pending , I want to change it to something else by using code,for example completed

3 Answers 3

3

Clive method doesn't work, probably he wanted to write

 $payments = \Drupal::entityTypeManager()->getStorage('commerce_payment')->loadByProperties(['order_id' => $order->id()]);

the other method would be to load it with loadMultiple

$payments = \Drupal::entityTypeManager()->getStorage('commerce_payment')->loadMultipleByOrder($order);

also @Interdruper method works and is slimmer as it first loads ids and not whole payments.

2

Try this:

use Drupal\commerce_payment\Entity\Payment;

$payments = \Drupal::entityQuery('commerce_payment')
  ->condition('order_id', $order->id())
  ->execute();

foreach ($payments as $value) {
  $payment = Payment::load($value);
  $payment->setState('completed')->save();
}
0

Load up the payments:

$payments = \Drupal\commerce_order\Payment\Payment::loadByProperties(['order_id' => $order->id()]);

Then loop through and save what you want to:

foreach ($payments as $payment) {
  $payment->setState('completed')->save();
}
2
  • The website encountered an unexpected error. Please try again later. Error: Class 'Drupal\commerce_order\Payment\Payment' not found in Drupal\razorpay\Controller\RazorpayController->hook() (line 36 of modules/custom/razorpay/src/Controller/RazorpayController.php).
    – Rifas Ali
    Commented Aug 8, 2018 at 11:11
  • Above is definitely not working (anymore),
    – Blissful
    Commented Oct 2, 2019 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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