0

I have a WooCommerce checkout page that makes an AJAX call to place an order when the form is submitted. I need to catch the value of the redirect URL in the success response and extract parameters and substrings from it into variables so I can build a different redirect URL to send to the payment gateway.

How can I use frontend jQuery on that page to access the value in the AJAX success response and do what I need?

I am really a beginner with Javascript, Ajax and jQuery so I need detailed guidance, please.

The AJAX response is (as can be found in the console):

{result: "success", redirect: "https://example.com/checkout/order-pay/1234/?key=wc_order_xyz"}

This is the frontend script I have right now:

<script>
    jQuery(document).ajaxSuccess(function(event) {
        event.preventDefault();

        //get Success response and extract substrings into variables...

        window.location.href = "new redirect URL"; //Build a new URL

    });
</script>

I need to build a new Redirect URL like this to use in the above function:

https://example.com/thank-you-page/wcf-key=wc_order_xyz&wcf-order=1234
AudioHead
  • 1
  • 1
  • When you say "need to extract parameters", do you mean to query strings, domain or what exactly? – Jose Rojas Sep 17 '19 at 19:41
  • I think you can find the direction following this link: https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript – Sergiu Costas Sep 17 '19 at 19:44

1 Answers1

1

You can use URL object from javascript, this way:

let url = new URL('https://example.com/checkout/order-pay/1234/?key=wc_order_xyz');

alert(url.protocol); // https:
alert(url.host);     // example.com
alert(url.pathname); // /checkout/order-pay/1234/
url.searchParams.forEach(item => alert(item)) //wc_order_xyz
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40