Drupal - Redirect to checkout after pressing the "Add to Cart" button

you could do something like this:

use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (strpos($form_id, 'commerce_order_item_add_to_cart_form_commerce_product') !== false) {
    $form['actions']['submit']['#submit'][] = 'MYMODULE_cart_alter_sub';
function MYMODULE_cart_alter_sub(&$form, FormStateInterface $form_state) {
  $responce =  new RedirectResponse('/cart');

There's an issue here to allow that through the formatter, it has a MR from me that we are using for a project. Note that it skips the cart completely, there is just checkout.


As there is no rules-based solution yet, I constructed a crappy jquery function to do this.

First, it checks whether Drupal has sent a message that you added a product to your cart, and then performs a simple redirect to the cart page, and there it submits directly to the checkout page.

  Drupal.behaviors.donate = {
    attach: function (context, settings) {
        if ($("div[role=contentinfo]").length){
              var str=$("div[role=contentinfo]").html();
              var n=str.includes('Donatie added to <a href="/cart">your cart</a>.');
              if (n){
        if ($("#edit-checkout").length){