How to perform custom javascript on place order but before redirection to payment gateway in Magento 1.9?

How to perform custom javascript on place order but before redirection to payment gateway in Magento 1.9?

September 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to perform custom javascript on place order but before redirection to payment gateway in Magento 1.9?

Integrating Custom JavaScript Before Payment Gateway Redirection in Magento 1.9

In this post, we’ll guide you through integrating custom JavaScript on the final step of the one-page checkout process in Magento 1.9 before redirecting to the payment gateway. We’ll ensure that your script only executes when a customer accepts agreements and places an order successfully.

Understanding the Checkout Process Flow

The Magento one-page checkout consists of several steps, with the last one being “Place Order”. This is where the order summary appears, along with any required terms & conditions checkboxes. Once the customer clicks “Place Order”, Magento processes the order data on the server side and redirects to the payment gateway if everything is valid.

Adding Custom JavaScript: The Basics

To add custom JavaScript to this specific step, you can use the layout XML file for the checkout review action. This will ensure your script is loaded only when a customer reaches the final order review step.

  1. Locate or create your theme’s local checkout.xml file in the following path:

    app/design/frontend/[Your_Package]/[Your_Theme]/layout/
    
  2. Add the following code inside the <default> layout:

<block type="core/text" name="custom_checkout_script">
    <action method="setText">
        <text><![CDATA[
            <script type="text/javascript">
                var webOrder = {
                    "id": shop_id,
                    "sum": order_value
                };
            </script>
            <script type="text/javascript" src="//service_address/before.js"></script>
        ]]></text>
    </action>
</block>
  1. Add the following reference line inside the <checkout_onepage_review> block to include your script:
<reference name="checkout.onepage.review">
    <block type="core/text" name="custom_checkout_script" as="custom_script" template="checkout/head.phtml"/>
</reference>

Ensuring Script Execution on Successful Order Placement

To guarantee the JavaScript only executes when an order is successfully placed, we need to wrap it within event listeners that wait for the “place_order_after” event.

  1. Create a new XML file named checkout.xml inside your theme’s local layout folder:
app/design/frontend/[Your_Package]/[Your_Theme]/layout/
  1. Add the following code:
<layout version="0.1.0">
    <default>
        <reference name="head">
            <action method="addJs">
                <script>js/jquery-1.8.3.min.js</script>
            </action>
        </reference>
    </default>
    <checkout_onepage_review>
        <block type="core/text" name="custom_checkout_script" as="custom_script" template="checkout/head.phtml"/>
    </checkout_onepage_review>
</layout>
  1. Create a new file named head.phtml inside your theme’s template folder:
app/design/frontend/[Your_Package]/[Your_Theme]/template/
  1. Add the following JavaScript code to head.phtml:
<script type="text/javascript">
    require(['jquery'], function($) {
        $(document).ready(function() {
            $('.button-place-order').click(function(e) {
                setTimeout(function() {
                    // Your script goes here
                }, 500);
            });
        });
    });
</script>

This code uses jQuery to listen for the “Place Order” button click event. It waits half a second (500 milliseconds) before executing your custom script, ensuring that the order placement process is complete.

Conclusion

By following these steps and utilizing Magento’s layout XML files along with event listeners, you can successfully integrate your custom JavaScript on the final checkout step before redirection to the payment gateway. This approach ensures that your script executes only when a customer accepts agreements and Magento successfully processes the order.

Remember to replace [Your_Package] and [Your_Theme] with your actual theme’s package name and template name throughout the paths mentioned in this post.

For more advanced customizations and optimization techniques, don’t hesitate to reach out to our team at Laravel Company, where we specialize in providing expert Magento development services tailored to meet your specific business needs.

Magento 1 Checkout Themes Orders

Our network