0

I have a checkout flow with two payment gateways, Credit Card (Stripe) and Pay Later (Manual Gateway).

The Stripe checkout flow works fine.

I overrode the fulfillment flow in mymodule.workflow.yml:

order_fulfillment:
 id: order_fulfillment
 group: commerce_order
 label: 'Fulfillment'
 states:
   draft:
     label: Draft
   awaitingpayment:
     label: Pay Later (Furikomi)
   fulfillment:
     label: Fulfillment
   completed:
     label: Completed
   canceled:
     label: Canceled
 transitions:
   place:
     label: 'Place order'
     from: [draft, awaitingpayment]
     to:   fulfillment
   paylater:
     label: 'Pay later'
     from: [draft]
     to:   awaitingpayment
   fulfill:
     label: 'Fulfill order'
     from: [fulfillment]
     to: completed
   cancel:
     label: 'Cancel order'
     from: [draft, fulfillment]
     to:   canceled

Now, when the payment method is "Pay Later", I am trying to execute the "paylater" transition so that the order does not get marked as ready for fulfillment.

In my event subscriber, I have this:

  public function onPlaceTransition(WorkflowTransitionEvent $event) {
    $order = $event->getEntity();

    $order_type = $order->bundle();

    $payment_method = NULL;
    $payment_gateway = NULL;
    if (!$order->get('payment_method')->isEmpty()) {
      $payment_method = $order->get('payment_method')->first()->entity->id();
    }
    if (!$order->get('payment_gateway')->isEmpty()) {
      $payment_gateway = $order->get('payment_gateway')->first()->entity->label();
    }

    if ($order_type === 'ABC') {
      // https://drupal.stackexchange.com/a/186333/5147
      if ($payment_gateway === "Pay Later") {
        // https://drupal.stackexchange.com/a/252624/5147
        // Set order status to "payment pending"
        $order_state = $order->getState();
        // Get the allowed transitions for the current state.
        $transitions = $order_state->getTransitions();
        $order_state->applyTransition($transitions['paylater']);
        $order->save();

However, this gives an error:

The website encountered an unexpected error. Please try again later. TypeError: Argument 1 passed to Drupal\state_machine\Plugin\Field\FieldType\StateItem::applyTransition() must be an instance of Drupal\state_machine\Plugin\Workflow\WorkflowTransition, null given, called in

I think the problem may be that I am trying to execute one state transition (paylater) inside the "onPlace" transition. Where can I modify the checkout flow so that I can move any order that chooses the "Pay Later" payment method to go through the "paylater" transition?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.