How to test or style the order success page?

95

38

If I reload the checkout/onepage/success page, I am directed to the cart.

So, when debugging or styling the order success page, I always have to make a new order.

How can I do this more efficiently?

Alex

Posted 2013-02-19T11:10:38.070

Reputation: 9 217

This question created varied answers which are all full of "win". Anyone visiting here should peruse all the answers. – Chris K – 2014-11-13T21:05:41.317

Answers

128

You can temporarily hack the core while you are developing:

In /app/code/core/Mage/Checkout/controllers/OnepageController.php edit successAction().

Comment out the line $session->clear();. Now you can make one order and refresh the page as often as you like.

If you do not even want to make an order on each browser, for example when doing cross-browser testing, you also can just initialize the session every time.

Pick an order id and a quote id from the table sales_flat_order (fields: entity_id and quote_id). For example via:

SELECT entity_id as order_id, quote_id 
  FROM sales_flat_order ORDER BY entity_id DESC LIMIT 1;

Then change the beginning of the function as follows:

$session = $this->getOnepage()->getCheckout();

$session->setLastSuccessQuoteId(INSERT_QUOTE_ID);
$session->setLastQuoteId(INSERT_QUOTE_ID);
$session->setLastOrderId(INSERT_ORDER_ID);

and replaceINSERT_.... with the IDs.

Now you can always call checkout/onepage/success

While you are at it, you might want to test the failureAction() as well, in

/app/code/core/Mage/Checkout/controllers/OnepageController.php

The modified action would look like this

public function failureAction()
    {
        $session = $this->getOnepage()->getCheckout();

        $session->setLastSuccessQuoteId(INSERT_QUOTE_ID);
        $session->setLastQuoteId(INSERT_QUOTE_ID);
        $session->setLastOrderId(INSERT_ORDER_ID);

        $lastQuoteId = $this->getOnepage()->getCheckout()->getLastQuoteId();
        $lastOrderId = $this->getOnepage()->getCheckout()->getLastOrderId();

        if (!$lastQuoteId || !$lastOrderId) {
            $this->_redirect('checkout/cart');
            return;
        }

        $this->loadLayout();
        $this->renderLayout();
    }

Now you can always call checkout/onepage/failure

Alex

Posted 2013-02-19T11:10:38.070

Reputation: 9 217

For people that would like to do the same in M2 you can comment lines 19 to 22 in \vendor\magento\module-checkout\Controller\Onepage\Success.php – Dynomite – 2017-03-13T12:27:07.607

Fixes this your problem? Please mark it as solution. – Fabian Blechschmidt – 2013-03-25T07:59:07.560

3I've tested both Jisse Reitsma's Yireo Checkout Tester and this method. While the module is the easiest way, I found it to be not 100% compatible with our theme and payment module, resulting in a different styling than the original succes page. While requiring to modify the core this method however worked like a charm. Note: the answer is missing the file you need to edit, which is /app/code/core/Mage/Checkout/controllers/OnepageController.php – Ottonet – 2015-06-20T22:19:36.173

25

You need to modify the successAction() in

/app/code/core/Mage/Checkout/controllers/OnepageController.php

Modified action would like this

public function successAction()
    {
        /*
        $session = $this->getOnepage()->getCheckout();
        if (!$session->getLastSuccessQuoteId()) {
            $this->_redirect('checkout/cart');
            return;
        }

        $lastQuoteId = $session->getLastQuoteId();
        $lastOrderId = $session->getLastOrderId();
        $lastRecurringProfiles = $session->getLastRecurringProfileIds();
        if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
            $this->_redirect('checkout/cart');
            return;
        }

        $session->clear();
        */
        $this->loadLayout();
        $this->_initLayoutMessages('checkout/session');
        Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));
        $this->renderLayout();
    }

blakcaps

Posted 2013-02-19T11:10:38.070

Reputation: 861

3But then $lastOrderId is not defined which would leat to a notice and to could lead to side effects in the template. – Alex – 2013-02-19T11:55:16.067

1I've had success just by commenting out the $session->clear(); The only thing I lose is any notices/messages added such as TBT/Rewards Notices/Messages added via observer. Leaving the code in a override module, which is .gitignored so never deployed. – Barry Carlyon – 2014-07-28T09:10:59.360

17

Hope I'm not too self-promoting, but I created a free extension that can be quickly installed in Magento, and allows you to preview the order success-page for any order - simply by accessing an URL: http://www.yireo.com/software/magento-extensions/checkout-tester

Regards.

Jisse Reitsma

Posted 2013-02-19T11:10:38.070

Reputation: 742

4

I think it's better to just comment $session->clear(); and add products manually, that worked for me but commenting the whole action gave me syntax errors.

Farzan Balkani

Posted 2013-02-19T11:10:38.070

Reputation: 141

3

Instead of sending out emails from local/development copy you can dump the content of the email to a file and then just see locally, which in my opinion will be really handy. Here's how that can be acheived. First all the emails are sent from

Mage_Core_Model_Email_Template::send($email, $name = null, array $variables = array())

Find that, and add the following lines.

    $this->setUseAbsoluteLinks(true);
    $text = $this->getProcessedTemplate($variables, true);

    if($this->isPlain()) {
        $mail->setBodyText($text);
    } else {
        $mail->setBodyHTML($text);
    }
    // Added Code //
    $filePath = Mage::getBaseDir() .  DS . 'email.html';
    Mage::log($filePath);
    file_put_contents($filePath, $text);
    // Added Code Ends //

    $mail->setSubject('=?utf-8?B?' . base64_encode($this->getProcessedTemplateSubject($variables)) . '?=');
    $mail->setFrom($this->getSenderEmail(), $this->getSenderName());

After this after you create any order an email.html will be created in the Magento document root and you can open that in browser to see the output.

Next, to send/re-send any order emails you can just log in admin and for each order there is Send Email button which will trigger this script and you can see the newly changed template written in the same file. I think this is one of the best way to see order or any other emails.

Please note to remove the added code when you are done.

Subesh Pokhrel

Posted 2013-02-19T11:10:38.070

Reputation: 681

3

For Magento 2:

If you want to style or customize in success page after order success page redirect to cart page.

Now solution is here:

Go To vendor/magento/module-checkout/Controller/Onepage open Success.php File.

In this file you see Below Code

 if (!$this->_objectManager->get('Magento\Checkout\Model\Session\SuccessValidator')->isValid()) {
     return $this->resultRedirectFactory->create()->setPath('checkout/cart');
 }

Just comment this code and your problem is solve. After comment this you dont redirect to cart page.

Nikul

Posted 2013-02-19T11:10:38.070

Reputation: 589

1

You have to update code:

/app/code/core/Mage/Checkout/controllers/OnepageController.php

Update function:

public function successAction()
{
    $session = $this->getOnepage()->getCheckout();
    if (!$session->getLastSuccessQuoteId()) {
        $this->_redirect('checkout/cart');
        return;
    }

    $lastQuoteId = $session->getLastQuoteId();
    $lastOrderId = $session->getLastOrderId();
    $lastRecurringProfiles = $session->getLastRecurringProfileIds();
    if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
        $this->_redirect('checkout/cart');
        return;
    }

    //$session->clear();

    $this->loadLayout();
    $this->_initLayoutMessages('checkout/session');
    Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));
    $this->renderLayout();
}

Just comment:

//$session->clear();

Ravi

Posted 2013-02-19T11:10:38.070

Reputation: 104

0

Magento 2

As another answer notes, you can comment out the redirect in Magento\Checkout\Controller\Onepage\Success::execute and force the checkout success page to load. But when the block, Magento\Checkout\Block\Onepage\Success, loads no order data will be present because Magento\Checkout\Model\Session::getLastRealOrder won't return an order. A better options would be an after interceptor on the controller class's execute method, where you can set a value for lastRealOrderId in the checkout session.

This approach also provides an opportunity to dispatch the checkout_onepage_controller_success_action event with your desired order. The event will trigger the observers Magento\GoogleAdwords\Observer\SetConversionValueObserver and Magento\GoogleAnalytics\Observer\SetGoogleAnalyticsOnOrderSuccessPageViewObserver with your test data.

The following is a fairly basic module creating the interceptor described above and allows setting the order by appending an order query parameter with the desired increment id to the success page url. It currently won't work the the multishipping success route. It can be downloaded https://github.com/pmclain/magento2-successtest

app/code/Pmclain/SuccessTest/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\Controller\Onepage\Success">
    <plugin name="pmclain_successtest_checkout_controller_onepage_success"
            type="Pmclain\SuccessTest\Plugin\Success"
            sortOrder="10" />
  </type>
</config>

app/code/Pmclain/SuccessTest/etc/Plugin/Success.php

<?php
namespace Pmclain\SuccessTest\Plugin;

use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Checkout\Model\Session;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\Order;

class Success
{
  /** @var ManagerInterface */
  protected $_eventManager;

  /** @var PageFactory */
  protected $_resultPageFactory;

  /** @var ScopeConfigInterface */
  protected $_scopeConfig;

  /** @var OrderFactory */
  protected $_orderFactory;

  /** @var Order */
  protected $_order;

  /** @var Session */
  protected $_checkoutSession;

  /**
   * Success constructor.
   * @param ManagerInterface $eventManager
   * @param PageFactory $resultPageFactory
   * @param ScopeConfigInterface $scopeConfig
   * @param OrderFactory $orderFactory
   * @param Session $session
   */
  public function __construct(
    ManagerInterface $eventManager,
    PageFactory $resultPageFactory,
    ScopeConfigInterface $scopeConfig,
    OrderFactory $orderFactory,
    Session $session
  ) {
    $this->_eventManager = $eventManager;
    $this->_resultPageFactory = $resultPageFactory;
    $this->_scopeConfig = $scopeConfig;
    $this->_orderFactory = $orderFactory;
    $this->_checkoutSession = $session;
  }

  /**
   * @param \Magento\Checkout\Controller\Onepage\Success $subject
   * @param $result
   * @return \Magento\Framework\View\Result\Page
   */
  public function afterExecute(\Magento\Checkout\Controller\Onepage\Success $subject, $result)
  {
    if (!$this->_isEnabled()) {
      return $result;
    }

    $order = $this->_getTestOrder($subject->getRequest()->getParam('order'));

    if (!$order->getId()) {
      return $result;
    }

    $this->_checkoutSession->setLastRealOrderId($order->getIncrementId());

    $resultPage = $this->_resultPageFactory->create();

    $this->_eventManager->dispatch(
      'checkout_onepage_controller_success_action',
      ['order_ids' => [$order->getId()]]
    );

    return $resultPage;
  }

  /**
   * @return bool
   */
  protected function _isEnabled()
  {
    if ($this->_scopeConfig->getValue('dev/debug/success_test', ScopeInterface::SCOPE_STORE)) {
      return true;
    }

    return false;
  }

  /**
   * @param $incrementId string|bool
   * @return Order
   */
  protected function _getTestOrder($incrementId)
  {
    /** @var Order $order */
    $order = $this->_orderFactory->create();

    $order->loadByIncrementId($incrementId);

    return $order;
  }
}

app/code/Pmclain/SuccessTest/etc/adminhtml/system.xml Below adds admin panel options to enable/disable the interceptor.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
  <system>
    <section id="dev">
      <group id="debug">
        <field id="success_test" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
          <label>Enable Checkout Success Page Testing</label>
          <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
        </field>
      </group>
    </section>
  </system>
</config>

app/code/Pmclain/SuccessTest/etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
  <default>
    <dev>
      <debug>
        <success_test>0</success_test>
      </debug>
    </dev>
  </default>
</config>

app/code/Pmclain/SuccessTest/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Pmclain_SuccessTest" setup_version="0.0.1">
    <sequence>
      <module name="Magento_Backend" />
      <module name="Magento_Checkout" />
    </sequence>
  </module>
</config>

app/code/Pmclain/SuccessTest/resgistration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'Pmclain_SuccessTest',
  __DIR__
);

Pmclain

Posted 2013-02-19T11:10:38.070

Reputation: 845