3

How can I perform a redirect after purchasing a product? I tried to create a custom checkout pane for this purpose, but it doesn't work as the form is not submited:

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  if ($this->getStepId() == 'complete') {
    $form_state->setRedirect('<front>');
  }
}

Is there any possibility to do this with a pane or do I have to create an event subscriber and redirect on the checkout event?

4 Answers 4

1

In your "submitForm" method write below code:

$form_state->setRedirect('<front>');
return;

To redirect you can also use the following code:

//after the namespace
use Drupal\Core\Url;

//In your "submitForm"
$url = Url::fromRoute('<front>');
$form_state->setRedirectUrl($url);

To redirect you can also use the following code:

//after the namespace
use Symfony\Component\HttpFoundation\RedirectResponse;

//In your "submitForm"
global $base_url;
$response = new RedirectResponse($base_url);
$response->send();
return;
1
  • this doesn't work. My submitPaneForm is not called as my buildPaneForm is empty and does not trigger the submit function
    – Torben
    Feb 21, 2019 at 13:59
1

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());
  }

}
0

I was able to achieve the redirect by setting the redirect in the submitPaneForm instead of the buildPaneForm function and attaching a JS to automatically submit the pane form.

I would prefer a solution without JS.

0

If you need this feature just for some product variations then install the Commerce Quick Purchase module and create a block for each of those variations and in the block settings define whether you want a customer Add to cart product at all or just redirect to a product page or a Shopping cart page or checkout page. See more: https://github.com/drugan/commerce_quick_purchase#redirection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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