Redirect user to a page after payment in shopify

Some_one
New Member
3 0 1

I send user to shopify with certain product number and quantity, which is being populated with price on shopify where user can make a payment,

Now i want the user to be redirected to a certian page in my website, after successful payment,

I created webhook order payemnt and given the url of the page to be redirected after payment, but it is not working

Please suggest

Replies 21 (21)
JesseF8
Shopify Staff
Shopify Staff
151 0 12

Hello!

Jesse from the Guru Team here.

While this would be considered a bit of hack you can achieve this with a bit of JS:

<script> window.location =" http://YOURURLHERE.COM "; </script>

Replace yoururlhere.com with your desired page, and place the code in the additional content and scripts section found in the Checkout section of the admin.

http://docs.shopify.com/support/configuration/store-customization/legacy-checkout/how-do-i-customize...

Feel free to reach out at any time if you have any other questions.

http://docs.shopify.com/support

support@shopify.com

Jesse | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

talaluca95
New Member
1 0 1

Jesse, THANKS SO MUCH. This was exactly what I was struggling to accomplish: to redirect users after a purchase.

So I pasted the code as you said, however it applies to ALL my products.

 

<script> window.location ="https://myurl.com/page"; </script>

 

Is there a way of discriminating? I would like that code to run for product B, but NOT for product A.

 

Thanks!!

itsolleh
New Member
3 0 0
Altin
Shopify Partner
7 0 4

Here's your full code:

 

 

try {
   let redirect_url = 'https://example-page-link.com/your-page';
   let redirect_prod_ids = [1651456314292, 1651496847236];

   let order_line_items = Shopify.checkout.line_items;
   for( var i=0; i<order_line_items.length; i++ ){
      if( redirect_prod_ids.includes( order_line_items[i].product_id ) ){
         window.top.location.href = redirect_url;
      }
   }
}catch(err){
   //if errors happen in the above code, do nothing
} 

 

 

redirect_url = the URL where you want to send the users;
redirect_prod_ids = put one or multiple product IDs separated by a comma (,) for which you want to redirect the users (works even if user has purchased multiple items on that particular order). You can get the product ID from your product's admin editor like so: 

You're welcome.

11 years of web development experience and recently into Shopify Apps.
stolzjj
New Member
3 0 0

Hello — could this be quantified by some sort of an "if/then" statement?

I have a collection where if someone purchases something from that collection, I'd like them to be redirected to a specific page to fill out a form with more information. 

 

Could I put the collection in there? Or should I list every individual product like Altin suggested above?

MADahmani
Shopify Partner
17 0 3

Thank you Altin.

BryceC
New Member
1 0 0

Where does the code go for this 

 

 

MADahmani
Shopify Partner
17 0 3

The code goes into Settings > Checkout > Order processing > Additional scripts

Also, don't forget to wrap the code between <script></script> tags.

tgoral
New Member
1 0 0

Thank you in advance for posting this.

 

Just to clarify, you go to "yoursite.myshopify.com/admin/settings/checkout" and under additional scripts, you add:

 

<script> window.location =" www.red.com"; </script>

 

It seems too easy. How can I test this script without having to make a payment?

 

 

BrianCraig
New Member
4 0 0

Hey Jesse, is it possible to pass the shopify customer id along with the URL as a dynamic parameter. Eg mystorepage.com?customerId={{customerid}}

 

Hope this make sense. Basically need an identifier for that customer to pass to Messenger

BrianCraig
New Member
4 0 0

Hey Atlin, do you know how to pass the customer id or email as an identifier along with the redirect?

 

Appreciate your help man!

dmroberts86
New Member
2 0 0

Okay, I customized a thank you page that I wanted to pop up after someone checks out on Shopify, however, it didn't work. I just went through and purchased the product myself. What did I do wrong? 

 

<script>
{
let redirect_url = 'https://www.theheroschool.com/pages/roadsto911-purchase-success-hero;
let redirect_prod_ids = [4381069410384, 4421280464976 ];
let order_line_items = Shopify.checkout.line_items;
for( var i=0; i<order_line_items.length; i++ ){
if( redirect_prod_ids.includes( order_line_items[i].product_id ) ){ window.top.location.href = redirect_url;
}
}
}catch(err){ //if errors happen in the above code, do nothing
}
</script>

dmroberts86
New Member
2 0 0

@Altin wrote:

Here's your full code:

 

 

try {
   let redirect_url = 'https://example-page-link.com/your-page';
   let redirect_prod_ids = [1651456314292, 1651496847236];

   let order_line_items = Shopify.checkout.line_items;
   for( var i=0; i<order_line_items.length; i++ ){
      if( redirect_prod_ids.includes( order_line_items[i].product_id ) ){
         window.top.location.href = redirect_url;
      }
   }
}catch(err){
   //if errors happen in the above code, do nothing
} 

 

 

redirect_url = the URL where you want to send the users;
redirect_prod_ids = put one or multiple product IDs separated by a comma (,) for which you want to redirect the users (works even if user has purchased multiple items on that particular order). You can get the product ID from your product's admin editor like so: 

You're welcome.



@Altin wrote:

Here's your full code:

 

 

try {
   let redirect_url = 'https://example-page-link.com/your-page';
   let redirect_prod_ids = [1651456314292, 1651496847236];

   let order_line_items = Shopify.checkout.line_items;
   for( var i=0; i<order_line_items.length; i++ ){
      if( redirect_prod_ids.includes( order_line_items[i].product_id ) ){
         window.top.location.href = redirect_url;
      }
   }
}catch(err){
   //if errors happen in the above code, do nothing
} 

 

 

redirect_url = the URL where you want to send the users;
redirect_prod_ids = put one or multiple product IDs separated by a comma (,) for which you want to redirect the users (works even if user has purchased multiple items on that particular order). You can get the product ID from your product's admin editor like so: 

You're welcome.


I can't seem to get this code to work. I contacted Shopify and they told me it won't because it's not allowed for individual products only as a group. I have a hard time with that comment and was wondering if there really is a way to make the code work? 

victoryma
New Member
3 0 0

It took me nearly all day, but I think I figured out a solution that works. It uses the Shopify checkout object selector @Altin provided.

 

I am linking to the stackoverflow post because I will not be available on this forum. However, I will try to be responsive to questions here https://stackoverflow.com/a/59726180/7353382

 

Keep in mind, this solution works uniquely for my Shopify experience because customers can only buy 1 product at a time. If your customers can add multiple items to their carts, than a for loop should likely be used.

 

<script>  
  const idUrlList = {
//change the lines below "product1": { "url": "https://www.URL-HERE.com", "product_id": 1234567898765, }, "product2": { "url": "https://www.URL-HERE.com", "product_id": 1234567898765, }, "product3": { "url": "https://www.URL-HERE.com", "product_id": 1234567898765, }, "product4": { "url": "https://www.URL-HERE.com", "product_id": 1234567898765, },
//change the lines above }; //do not remove this line let productPurchasedId = Shopify.checkout.line_items[0].product_id; for (const product in idUrlList) { if (idUrlList[product].product_id === productPurchasedId) { window.location.replace(`${idUrlList[product].url}`); } else { console.log(`Found no matches in dictionary. The dictionary ID is ${idUrlList[product].product_id} The product ID is ${productPurchasedId}`); }; }; </script>

 

To make this work for you, make the following replacements:

  1. Replace product1 with the name of the product or redirect that you will find memorable. For example, in one of mine I wrote 20_off_2020.
  2. Replace https://www.URL-HERE.com with the link you want people to go to. For example, https://www.pulse-creative.com/
  3. Replace 1234567898765 with the unique product ID that you want to check for. This can be found by going to Products, clicking on a product, and grabbing the string of numbers at the end of the URL.
  4. Add/remove the other products  depending on how many products you have. If you only have two products, remove the code from "product3" all the way up to the line which says //change the lines above.

The rest of this code will automatically handle finding product IDs and handling their correct redirects.

 

victoryma
New Member
3 0 0

Not sure if my earlier post showed up? I found a solution to redirect people to different URLs based on the product ID.

 

Code available at https://stackoverflow.com/a/59726180/7353382

furrydeath
Tourist
6 0 0

How would I include the customer's first name, last name and email address in the redirect URL. For example:

 

window.location ="https://www.mysite.com/order-success?firstname=John&lastname=Smith&email=jsmith@example.com";

Do I have access to liquid variables here, or is liquid disabled on checkout pages?

Hadarinc
New Member
3 0 0

Hi,

This is not working for me.


When I am testing the payment with a third-party (sandbox mode) it redirects me to the checkout page?

 

Please help


@JesseF8 wrote:

Hello!

Jesse from the Guru Team here.

While this would be considered a bit of hack you can achieve this with a bit of JS:

<script> window.location =" http://YOURURLHERE.COM "; </script>

Replace yoururlhere.com with your desired page, and place the code in the additional content and scripts section found in the Checkout section of the admin.

http://docs.shopify.com/support/configuration/store-customization/legacy-checkout/how-do-i-customize...

Feel free to reach out at any time if you have any other questions.

http://docs.shopify.com/support

support@shopify.com


 

Hadarinc
New Member
3 0 0

Hi Guru Team,

Please help me here. I am using the Shopify Buy button on my page. When customer is redirected after making payment, the redirect is taking them back to the shopify page (which is not setup) and not the page where the buy button is on. I have been trying to use the "additional scripts" under checkout settings but it is not working. 

 

What is the exact code that I need to use along with my URL?

 

Please help URGENT!!

@JesseF8 wrote:

Hello!

Jesse from the Guru Team here.

While this would be considered a bit of hack you can achieve this with a bit of JS:

<script> window.location =" http://YOURURLHERE.COM "; </script>

Replace yoururlhere.com with your desired page, and place the code in the additional content and scripts section found in the Checkout section of the admin.

http://docs.shopify.com/support/configuration/store-customization/legacy-checkout/how-do-i-customize...

Feel free to reach out at any time if you have any other questions.

http://docs.shopify.com/support

support@shopify.com


 

boolangery
Tourist
16 0 1

I just created a public app for one of my client with this purpose, if you want to give a try: https://apps.shopify.com/redirect-after-checkout