I know that you can easily achieve this functionality in Drupal 7 with the Rules module or using the Commerce Checkout Express module.

Since Rules and Commerce are not that much integrated yet, I am not sure we can achieve this that easily, in Drupal 8.

Can I implement this feature using just rules? If the answer is no, how can I do it, programmatically?

  • 1
    It's certainly easy to achieve, but the qualifying question in response is what have you already researched / tried that didn't work? – Ryan Szrama Jun 11 '17 at 20:38
  • @RyanSzrama Thanks for your interest Ryan, So far I tried using Rules with the event of "When a new order is saved" which didn't really work, tried to listen with event dispatcher when the item is added to cart but I failed miserably, and I googled around and found pretty much no solution for this – Sohail Jun 13 '17 at 8:12
  • 1
    I pinged mglaman and apparently we have an open issue to support this in the queue; I don't have it handy, but I'm hoping he can kick us a link. : P – Ryan Szrama Jun 16 '17 at 17:02

If you use the last development version of commerce, and look for file commerce\modules\cart\src\Form\AddToCartForm.php which lives under the Drupal\commerce_cart\Form namespace, you can see that cart_id was set, and it is the order ID too.

$form_state->set('cart_id', $cart->id());

Therefore, in hook_form_alter(), we can get the order ID (the cart ID).

$order_id = $form_state->get('cart_id');

The complete code would be this.

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

 * Implements hook_form_alter().
function <MY_MODULE>_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    // Add to cart form id for product 5 is: commerce_order_item_add_to_cart_form_commerce_product_5
    // Then it's enough to check $form_id contains the string 'commerce_order_item_add_to_cart_form_commerce'
    if(strpos($form_id, 'commerce_order_item_add_to_cart_form_commerce') !== false) {
        $form['actions']['submit']['#submit'][] = '_redirect_to_checkout';

// Callback for form submission
// @see hook_form_alter().
function _redirect_to_checkout(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\commerce_order\Entity\OrderInterface $cart_id */
  $cart_id = $form_state->get('cart_id');

  $url = Url::fromRoute('commerce_checkout.form',
    ['commerce_order' => $cart_id]

  • $form_state->setRedirectUrl($url); requires a Url object not a string. See the ref docs for the function. – Duncanmoo Sep 11 '17 at 13:56
  • When using this code, I get a "Error: Call to a member function getPlugin() on null in /modules/contrib/commerce/modules/payment/src/PaymentListBuilder.php on line 90" when navigating on my order edit page and clicking on Payment, it seems that this redirection breaks thie payment reference on the order... – Flo Nov 13 '17 at 15:58

Your Answer

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

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