Conditional Upsell/Downsell depending on product bought?

Thanks for reviving this thread @Vtkv , here is how to do the above:

Step 1: Checkout code

Add this code as it is into your Checkout page (Custom Codes > Body JS):

<script>
document.querySelectorAll(`input[name="product-id"]`).forEach(x=>{
    x.addEventListener('change',()=>{
        let ids = [...document.querySelectorAll(`input[name="product-id"]:checked`)].map(x=>x.value)
        
        localStorage.setItem("_all_selected_products", JSON.stringify(ids))
    })
})
</script>

The code above will store the currently selected product(s) into localStorage, that way we know what the customer bought in the next steps.

Step 2: Upsell(s) code

Add this code into the Upsells/Downsells you want to skip (only).

<script>

// Change the line below to include your product IDs from the checkout page, when any of the product IDs
// mentioned were bought the code will automatically redirect to the given STEP_URL_TO_JUMP_TO URL.
const CHECKOUT_PRODUCT_IDS_TO_SKIP = ["216171","another product id here...etc"];

// Here provide the next step URL you'd like to take your customers to when skipping the current one.
const STEP_URL_TO_JUMP_TO = "my next upsell or thank you page to go though when skipping the current one.";

try {
    let idsStr = localStorage.getItem("_all_selected_products")
    if (idsStr) {
        let ids = JSON.parse(idsStr)
        if (ids.find(x=>CHECKOUT_PRODUCT_IDS_TO_SKIP.find(y=>x===y))) {
            window.location = STEP_URL_TO_JUMP_TO      
        }
    }
} catch {}
</script>

Hope that helps, do let me know if the code does not work or facing any issues…

@yassine

3 Likes