0

I am working on a client's site. I have a requirement that when a user buys a product, a specific role should be assigned to the user. For this, I have used 'business rules' module.
Business Rules
Reacts on event: After updating Entity
Target Entity Type: Order
Target Bundle: Default
Condition
Type: Data Comparison
Target Entity Type: Order
Target Bundle: Default
Field: state: State[state]
Data to compare: Current value
Operator: =
Value to compare: completed
Items to execute if condition succeed
Action: Add 'One year subscriber' role to user

My question is, Is this the correct way to identify that the product is purchased and order is paid in full?
That is, if order status is completed then is it safe to assume that the product is purchased and order is paid in full?

1 Answer 1

2

No. As with Commerce 1.x, we have a specific event in Commerce 2.x that is only triggered the first time an order is observed to be paid in full. It's defined in OrderEvents.php:

  /**
   * Name of the event fired after the order has been fully paid.
   *
   * Guaranteed to only fire once, when the order balance reaches zero.
   * Subsequent changes to the balance won't redispatch the event (e.g. in case
   * of a refund followed by an additional payment).
   *
   * Fired before the order is saved.
   *
   * @Event
   *
   * @see \Drupal\commerce_order\OrderInterface::getBalance()
   * @see \Drupal\commerce_order\Event\OrderEvent
   */
  const ORDER_PAID = 'commerce_order.order.paid';
2
  • This is an event-ful way to determine paid status. Is there a stateful way? ie. how can I tell from a instantiated order object whether it's paid? Commented Sep 2, 2022 at 19:50
  • 1
    @aaronbauman if you're talking about 1.x, it's commerce_payment_order_balance(). If you're talking about 2.x, there's a total_paid field on the order you can compare against the total_price. Commented Sep 5, 2022 at 3:21

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.