AJAX Add-to-Cart

Job ID: 39894090

Budget: $10 – $30 AUD

I want a clean, code-only solution that lets me drop a text link onto any product page so a different product can be added to the cart without leaving the page. The storefront runs the default product template and currently has no custom scripts or third-party apps installed, so the code you supply must be self-contained.

Here’s the exact flow I need:

Product A page loads → visitor clicks a small “Add free sample” (or any text I set) → Product B is added to the cart quietly in the background → the visitor remains on Product A’s page and the cart icon/mini-cart updates.

I’m comfortable editing the theme code myself, so I only need:

• A lightweight JavaScript (or Liquid + JS) snippet that calls Shopify’s AJAX Cart API to add a specific product/variant ID.
• Clear instructions on where to place the snippet and how to swap in different product IDs or link texts later.
• Optional: a brief line on how to display a success message or toast, though a silent add is fine if you prefer.

No page redirects, no additional libraries—vanilla JS is ideal. Once I drop your snippet in and test, clicking the link should add the item instantly and keep the user on the same page.