Examining CheckoutFlowBase::redirectToStep() taught me that this could be accomplished in a checkout pane like this:
use Drupal\commerce\Response\NeedsRedirectException;
use Drupal\Core\Url;
...
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
throw new NeedsRedirectException(Url::fromUri('internal:/mydestination')->toString());
}
Here's a configurable checkout pane for this purpose:
<?php
namespace Drupal\your_module\Plugin\Commerce\CheckoutPane;
use Drupal\commerce\Response\NeedsRedirectException;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Provides the redirection pane.
*
* @CommerceCheckoutPane(
* id = "redirection",
* label = @Translation("Redirect"),
* )
*/
class Redirection extends CheckoutPaneBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'destination' => 'internal:/',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationSummary() {
return $this->configuration['destination'];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['destination'] = [
'#type' => 'textfield',
'#title' => $this->t('Redirect to'),
'#description' => "URI of the destination. For example 'internal:/search' or 'http://google.com'.",
'#default_value' => $this->configuration['destination'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state->getErrors()) {
$values = $form_state->getValue($form['#parents']);
$this->configuration['destination'] = $values['destination'];
}
}
/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
throw new NeedsRedirectException(Url::fromUri($this->configuration['destination'])->toString());
}
}