Different redirect URLS for different products purchased

Topic summary

Goal: redirect customers to different URLs after checkout based on which product was purchased.

  • Original approach: a single redirect works for one product, but duplicating the code and changing product ID/URL still redirects to the original URL. Code snippets (JavaScript on the checkout page using Shopify.checkout.line_items) are central to the issue.

  • Proposed solution: use a mapping of product IDs to redirect URLs in a single script. Iterate over Shopify.checkout.line_items, check if the product_id exists in the mapping, then set window.top.location.href to the mapped URL. Include a break to ensure only one redirect occurs.

  • Technical elements: object/dictionary keyed by product_id; iteration over line items; single redirect execution to avoid conflicts.

  • Latest update: a participant asked how to add a 5-second delay before redirecting. No answer provided yet.

  • Status: proposed code-based mapping solution offered; adoption/confirmation not reported. Delay/timer question remains open.

Summarized with AI on December 21. AI used: gpt-5.

I need help trying to create separate redirect URLs when different products are purchased. Currently, I use this code to redirect after this specific product is purchased, and it works. I tried duplicating the code and changing the URL and product ID, but it redirects to the URL in the original code.

<script> window.location ="https://app.squarespacescheduling.com/schedule.php?owner=27901056&appointmentType=40249668 "; </script>
try {
   let redirect_url = 'https://app.squarespacescheduling.com/schedule.php?owner=27901056&appointmentType=40249668';
   let redirect_prod_ids = [8132767121716];
   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
} 

I’m not very familiar with coding so any help would be great!

The issue you’re facing with your current approach likely arises because the script is not adequately set up to handle multiple products and their corresponding redirect URLs. To solve this, you need to modify the script to map each product ID to its respective redirect URL. Here’s how you can do it:

Hi There. how can I set a timer to wait 5 seconds before redirecting?