All things Shopify and commerce
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:
<script>
try {
let redirects = {
8132767121716: 'https://app.squarespacescheduling.com/schedule.php?owner=27901056&appointmentType=40249668',
// Add more product IDs and their URLs here
// Example: 1234567890123: 'http://example.com/another-product-redirect'
};
let order_line_items = Shopify.checkout.line_items;
for (let i = 0; i < order_line_items.length; i++) {
let productId = order_line_items[i].product_id;
if (redirects.hasOwnProperty(productId)) {
window.top.location.href = redirects[productId];
break; // This ensures that the redirection happens only once
}
}
} catch (err) {
// Error handling or logging
}
</script>
Hi There. how can I set a timer to wait 5 seconds before redirecting?
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024