Stopping Redirection on Order Placement in Magento 1 Checkout
As a new developer getting started with Magento, it’s common to want custom control over certain parts of the checkout process. In this post, we’ll guide you through how to stop the automatic redirection that occurs when clicking the “Place Order” button and instead display a message or block directly on the page.
Understanding the Default Checkout Flow
First, let’s understand what happens by default in Magento 1’s onepage checkout:
When you click “Place Order”:
- The
checkout_onepage_controller_index_saveOrderaction is triggered. - This action processes the order and redirects to either a success or failure page depending on the results.
Stopping the Redirection Flow
To stop this redirection behavior, we need to override the core controller action that handles the order placement process. Here’s how:
Step 1: Create a Custom Checkout Module
If you haven’t already, create a custom module for your theme or extension. For this example, let’s call it MyCompany_Checkout.
Step 2: Override the Save Order Action
In your module’s controller folder (app/code/local/MyCompany/Checkout/controllers), copy and paste the core file OnepageController.php. Name your copy something like MyOnepageController.php.
Step 3: Modify the Save Order Method
Find the line:
$this->getResponse()->setRedirect($this->getUrl('checkout/onepage/success', array('_secure'=>true)));
Replace it with:
// Stop redirection and display a message block instead
Mage::app()->getFrontController()->getAction()->getResponse()
->setBody('<div style="color:green;">Your order has been successfully placed!</div>');
This line stops the redirection by not setting a redirect URL, and instead directly sets HTML content in the response body.
Step 4: Clear Magento Cache
After saving your changes, make sure to clear all Magento cache types (System > Cache Management).
Displaying a Custom Block Instead of Redirection
For a more elegant solution than hardcoding an HTML message, let’s display a custom block of content:
Step 1: Create a New Checkout Template
In the app/design/frontend/<your_package>/<your_theme>/template/checkout/ folder, create a new file called onepage/paymentmethod.phtml.
Step 2: Add Your Custom Block Content
Open this new file and add your desired block content. For example:
<div id="my-order-success-block" style="display: none;">
<h1>Your Order Has Been Successfully Placed!</h1>
<!-- Add any custom HTML or blocks here -->
</div>
Step 3: Update the Save Order Method
Replace your previous line in MyOnepageController.php with:
// Stop redirection and display a message block instead
$this->_redirect('*/*/');
Mage::helper('core')->jsPrintInlineScript("jQuery(document).ready(function() {
jQuery('#my-order-success-block').show();
});");
Step 4: Update the Onepage Block
In your theme’s app/design/frontend/<your_package>/<your_theme>/layout/local.xml, update or add a new XML block:
<reference name="checkout.onepage">
<action method="insert">
<block type="core/text" name="my.order.success.block" as="my_order_success_block"/>
</action>
</reference>
This will display your custom success message block right below the “Place Order” button.
Conclusion
By following these steps, you can now control what happens when a customer clicks the “Place Order” button in Magento 1’s onepage checkout. You’ve learned how to stop the default redirection and instead display either an inline HTML message or a custom block of content directly on the page.
Remember to always clear your cache after making these changes, and ensure you have the necessary permissions to modify core files and create new blocks.
For more advanced customizations like adding form validation or processing additional order data before placement, consider exploring Magento’s event-observer pattern. You can find detailed guides on this at Laravel Company.
Stay tuned for upcoming posts on further enhancing your Magento checkout experience!