0

I've created a custom checkout pane by extending Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase and added it to the "Order information" step in a checkout flow. This accepts customer data.

The customer then progresses to the "Review" stage to confirm everything is correct. The customer can "Edit" their information if required, going back to the "Order information" step.

Within the custom pane I'm wanting to determine whether it's being displayed in the "Order information" step or the "Review" step.

getStepId() always returns what's in the config for the pane. In this case, "Order information".

$this->order->checkout_step seems to always return "Review", or maybe it's just reporting the highest step reached in the order thus far.

Is there a Commerce native way of determining the exact step being viewed? Or will I need to check the URL directly?

Edit: I've tried checking the route name, but both /checkout/[id]/order_information and /checkout/[id]/review return commerce_checkout.form.

2 Answers 2

1

A good place to look for example code related to the checkout form is CheckoutController.php. Its formPage() function ensures the form for the correct checkout page is built by comparing the checkout step requested in the URL to the checkout step the order last advanced to, ensuring the customer isn't trying to directly navigate to a later step than it's entitled to.

You can see that it uses the a route matcher to get the named 'step' parameter from the URL, and you can do the same thing in your own code. Typically you'd inject the route matcher into the checkout pane class, but a quick and dirty solution to fetch the current route matcher from the container is:

$container = \Drupal::getContainer();
$route_matcher = $container->get('current_route_match');
$step_id = $route_matcher->getParameter('step');

This will functionally do what you've done by looking at the URL directly, to be honest, but it's a bit more structured way of doing so.

1
  • 1
    Thanks. It's certainly more robust than inspecting the URL directly.
    – imclean
    Jun 10, 2020 at 4:47
0

Checking the path does work but I'm not it's ideal.

$current_uri = \Drupal::request()->getRequestUri();
$path_elements = explode('/', $current_uri);
if (end($path_elements) === 'review') {
  // Do something.
}

Your Answer

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

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