Articles


Drupal Commerce: How to redirect to a specific page via Rules?

Drupal Commerce: How to redirect to a specific page via Rules?



Posted byUttam Kotekar,30th Jul 2016

In this article we will explain how to redirect to checkout page when you place an order and payment transaction gets failed. There are several ways via code to implement this but via Rules its pretty cool. Default rules module itself provides an action to add redirect to an specific page. Lets begin how we can achive this. Inorder to execute this rule only on the checkout flow process not on Admin UI order process we are using path rules module. You need to enable this before importing below the rule.

1) Initially we will create an event name called "test" and event will be "After saving a new commerce payment transaction".
2) This rule needs to be trigger only when the payment transaction is failed and also needs to be checked whether its in the checkout flow process.
So first lets create an condition "Data comparison" and the selected data will be "commerce-payment-transaction:status".  Operator will be "is one of" and the data value will be "Failure".
Secondly to check whether its on the checkout flow process, add another condition "Check path" and path to check will be "checkout" and the Comparison operation is "Contains".
3) So finally lets create an actions: Add an action "Page redirect" and the URL value will be "checkout/[commerce-payment-transaction:order]" and save it.

Thats it. Check the functionality it work like charm.

{ "rules_test" : {
    "LABEL" : "test",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "pathrules", "entity" ],
    "ON" : { "commerce_payment_transaction_insert" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "commerce-payment-transaction:status" ],
          "op" : "IN",
          "value" : { "value" : { "failure" : "failure" } }
        }
      },
      { "pathrules_checkpath" : { "path" : "checkout" } }
    ],
    "DO" : [
      { "redirect" : { "url" : "checkout\/[commerce-payment-transaction:order]" } }
    ]
  }
}

Categories