Featured: How to properly calculate FPT/WEEE on configurable products in Magento 2

Latest article one-liner

Read the full article

Ask Me Anything

Got a webdevelopment related question?
Ask me, and I'll generate an in-depth article about it.

Ask a question

Long Reads

Grab a cup of coffee and let me tell you something …

* grabs coffee *

Latest article:


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

How to, 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.


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.

Featured categories:

Magento - It's one of the most used e-commerce solutions out there today. Here's my 2 cents about it. (52 articles)

Webdevelopment - Making the internet a more beautiful place day by day since 2006. (53 articles)

Long reads - Sit back, grab a coffee and enjoy some of my more in-depth articles about stuff that moves me. (5 articles)