·

photo credit: www.cafecredit.com

How to style the checkout/onepage/success page in Magento 2

It’s a common problem: you want to style, template or test the order success page, but each time you refresh the page Magento 2 redirects you to the cart page. This is the default behaviour of Magento 1 & 2: when you’re order is complete, you’re only allowed to see this page once. Refreshing the page means that you’re redirected away from it. This makes it quite hard to style or test this page without having to create fake orders each time.
There is a method to disable this redirect. There are various article on how to do this for Magento 1, but I haven’t found a simple one for Magento 2. So I decided to write one myself.

Let’s take a peek under the hood

If we look at the controller that’s responsible for rendering the success page (Magento\Checkout\Controller\Onepage\Success ), we see a very interesting line:

This line is the line that causes the redirect. The Magento\Checkout\Model\Session\SuccessValidator::isValid() -method returns either true  or false , and if it returns false  we get our redirect. So let’s just take a look at this function:

Now this is something we can work with! Why? Because it’s a public method! And we can make use of Magento 2’s Interceptors (plugins) to modify the output of this function.

The plugin

The first thing we need to do, is add (or edit) our modules’ file etc/frontend/di.xml :

Secondly, we need to create our plugin file Plugin\Magento\Checkout\Model\Session\SuccessValidator.php :

There are some interesting things to note from this script.

  • First of all, we need to be able to communicate with the checkout session, so we create a protected variable called $checkoutSession .
  • Secondly we want our page to just show the latest completed order. For that purpose we create an order collection and just filter and sort it so we get our last completed order.
  • And last but not least, when an order is found, we override the parameters of our checkout session so that Magento’s native code is fooled by our plugin.

Voila! You can now refresh the order success page as much as you want.

Improvements

Needless to say, this script is purely used for development / styling / testing purposes because it has one obvious flaw: it will always load an order from the database and will never use it’s native functionality. To fix this flaw you need to wrap everything in afterIsValid() , except the last line, so you can execute the code when some requirements are met. For example, if you only want it to work when you add ‘?test’ to the URL:

Or if you want to make use of system configuration:

Well, I think you’re smart enough to figure it out! I hope this article helps you.

Visitors give this article an average rating of 4.8 out of 5.

How would you rate this article?

6 thoughts on “How to style the checkout/onepage/success page in Magento 2”

  1. Gaya says:

    Just what I needed to win the game!

  2. Manish says:

    Thanks,

    Nice article this help me a lot.

  3. Elena says:

    Hello. This code does not work when there are no one order.

  4. Tommie says:

    Thanks for all this useful information!
    We used various methods, however, the easiest way to have the checkout page customized is to use a free or paid extension. Hopefully there are plenty of companies who offer those.
    For Magento 2 we used Checkout Success Page module by Plumrocket. Now styling checkout success page is a piece of cake for us!

  5. idham says:

    hey, how to override the layout for checkout success page?

  6. Luka says:

    Why not just comment out the line in Magento\Checkout\Controller\Onepage\Success ($session->clearQuote(); ) and when you’re done, revert it back?

Leave a Reply