Redirect user to a page after payment in shopify

New Member
3 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

0 Likes
New Member
3 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!

0 Likes
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>

0 Likes
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? 

0 Likes
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.

 

0 Likes
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

0 Likes