Auto-Add Free Products to Shopify Cart

If you want to add a product to your Shopify cart automatically when another product is added, and you want the new product to have the same quantity as the first one, this tutorial will show you how.

In this guide, we’ll go through the steps to automatically add a second product to the cart when the first one is added. We will also ensure that the second product gets the same quantity as the first product.

Steps:

  1. Get Cart Information
    First, we need to get the cart details using Shopify’s /cart.js API. This gives us all the items in the cart, including the product IDs and quantities.

  2. Find the First Product
    Next, we check if the first product (the one that triggers the action) is already in the cart. We use the product ID to find it.

  3. Check for the Second Product
    We then check if the second product (the one we want to add automatically) is already in the cart. If it’s not, we proceed to add it.

  4. Add the Second Product
    If the second product is not in the cart, we will automatically add it using Shopify’s /cart/add.js API. We also ensure that the quantity of the second product is the same as the quantity of the first product.

  5. Refresh the Cart
    After the second product is added, we refresh the page to update the cart with the new product.

The Code:

Here is the code that will do all of this:

<script>
fetch("/cart.js")
.then((res) => res.json())
.then((cart) => {
const items = cart.items;

// Check if the first product is in the cart
const productInCart = items.find(item => item.product_id === 7650687647902);

// Check if the second product is already in the cart
const secondProductInCart = items.find(item => item.product_id === 8356958601374);

if (productInCart) {
// Get the quantity of the first product
const productQuantity = productInCart.quantity;

// If the second product is not in the cart, add it with the same quantity
if (!secondProductInCart) {
fetch('/cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
items: [{
id: 45729011663006, // Variant ID of the second product
quantity: productQuantity // Same quantity as the first product
}]
})
})
.then((res) => res.json())
.then(() => {
// Refresh the page after adding the product to the cart
location.reload();
})
.catch((error) => console.error("Error adding product:", error));
}
}
})
.catch((error) => console.error("Error fetching cart:", error));
</script>


Explanation:

  1. Get Cart Details: We fetch the cart using /cart.js to get all items and their details (like product ID and quantity).

  2. Find First Product: We check if the first product is in the cart by searching for its product_id.

  3. Find Second Product: We then check if the second product is already in the cart. If it is not, we add it.

  4. Same Quantity: The quantity of the second product is set to be the same as the first product’s quantity.

  5. Refresh: After the second product is added, we refresh the page to update the cart.

Why This is Useful:

This solution is great for situations where you want to offer a “bonus” product to customers when they add a certain product to their cart. For example, if they add a “poultry feeder,” you can automatically add a “chicken egg brush” to their cart with the same quantity. This makes the shopping experience smoother and can help increase sales.

Conclusion:

By using this method, you can automatically add a second product to the cart in Shopify, and make sure it has the same quantity as the first product. This is a simple way to improve the customer experience on your store, and it doesn’t require complicated code.

How We Built a Dynamic Add to Cart Button on Shopify (With Code + Demo Links)

Hey friends! 👋

This time, we’re not just talking about how we built a smart Add to Cart button — we’re showing it with demo links and working code.

Let’s say your store sells pet food in Small, Medium, and Large sizes. You also offer subscriptions, and you want to throw in a freebie too. Here’s how we made everything click together.


🧪 DEMO Add to Cart + Buy Now Links

Let’s use the following demo values:

  • Product Variant IDs:

    • Small: 1234567890

    • Medium: 1234567891

    • Large: 1234567892

  • Free Product Variant IDs:

    • Small: 2234567890

    • Medium: 2234567891

    • Large: 2234567892

  • Subscription Plan ID: 87654321

  • Quantity: 2

🛒 Add to Cart link:

https://webiste.com/cart/add?items[0][id]=1234567891&items[0][quantity]=2&items[0][selling_plan]=87654321&items[1][id]=2234567891&items[1][quantity]=1

Buy Now link (goes straight to checkout):

https://webiste.com/cart/clear?return_to=%2Fcart%2Fadd%3Fitems%5B0%5D%5Bid%5D%3D1234567891%26items%5B0%5D%5Bquantity%5D%3D2%26items%5B0%5D%5Bselling_plan%5D%3D87654321%26items%5B1%5D%5Bid%5D%3D2234567891%26items%5B1%5D%5Bquantity%5D%3D1%26return_to%3D%2Fcheckout

💡 JavaScript Code (Copy and Paste)

Here’s the full code that does all the magic:

<!-- Buttons -->
<div class="cart-btn" style="margin:20px 0px;">
<a id="cart-url-output" href="#" class="add-cart-custom" target="_blank">Add to cart</a>
<a id="checkout-url-output" href="#" class="buy-now-custom" target="_blank">Buy Now!</a>
</div>
$(document).ready(function () {
let productData = {
size_small: '1234567890',
size_medium: '1234567891',
size_large: '1234567892'
};

let freeProductData = {
size_small: '2234567890',
size_medium: '2234567891',
size_large: '2234567892'
};

function getSelectedSizeValue() {
return $('.all-inputs-sizes input[name="option2"]:checked').val();
}

function getSelectedQuantity() {
let qty = parseInt($('#qty-input').val(), 10);
if (isNaN(qty) || qty < 1) qty = 1;
return qty;
}

function getSelectedSubscription() {
const selectedSub = $('.subscription-option.selected');
return {
frequency: selectedSub.data('frequency'),
subscriptionValue: selectedSub.data('subscription-value')
};
}

function generateURLs() {
const selectedSize = getSelectedSizeValue().toLowerCase();
const quantity = getSelectedQuantity();
const subscription = getSelectedSubscription();

const productSizeID = productData[`size_${selectedSize}`];
const freeProductSizeID = freeProductData[`size_${selectedSize}`];

const cartURL = `https://waggyy.com/cart/add?` +
`items[0][id]=${productSizeID}&` +
`items[0][quantity]=${quantity}&` +
`items[0][selling_plan]=${subscription.subscriptionValue}&` +
`items[1][id]=${freeProductSizeID}&` +
`items[1][quantity]=1`;

const checkoutURL = `https://waggyy.com/cart/clear?return_to=` +
encodeURIComponent(`/cart/add?items[0][id]=${productSizeID}&items[0][quantity]=${quantity}&items[0][selling_plan]=${subscription.subscriptionValue}&items[1][id]=${freeProductSizeID}&items[1][quantity]=1&return_to=/checkout`);

$('#cart-url-output').attr('href', cartURL);
$('#checkout-url-output').attr('href', checkoutURL);
}

function refreshSelectionsAndURLs() {
generateURLs();
}

// Simulated click/select events
$('.all-inputs-sizes input[name="option2"]').on('change', refreshSelectionsAndURLs);
$('.subscription-option').on('click', function () {
$('.subscription-option').removeClass('selected');
$(this).addClass('selected');
refreshSelectionsAndURLs();
});

$('#qty-increase').on('click', function () {
let qty = getSelectedQuantity();
$('#qty-input').val(qty + 1);
refreshSelectionsAndURLs();
});

$('#qty-decrease').on('click', function () {
let qty = getSelectedQuantity();
if (qty > 1) {
$('#qty-input').val(qty - 1);
refreshSelectionsAndURLs();
}
});

$('#qty-input').on('input change', function () {
refreshSelectionsAndURLs();
});

// First load
refreshSelectionsAndURLs();
});

✅ What You Need to Make This Work

  1. Replace the variant IDs with your own from Shopify admin.

  2. Set your own selling_plan IDs from your subscription app.

  3. Add simple radio buttons for size and subscription in HTML.

  4. Add quantity input field with increase/decrease buttons.

Final Thoughts

This setup makes life easier for your customers — and you! It removes steps, adds flexibility, and works great with Shopify’s cart system. Try it on your store, or ask us if you get stuck — we’re happy to help! 💬

Shopify Thank You & Order Status Pages Update – What You Need to Do

Shopify has announced an important update for non-Plus merchants regarding the Thank You and Order Status pages. By August 26, 2026, all stores must migrate their customizations to the new system. The old method using Additional Scripts and ScriptTags will no longer work.

What Are the Thank You & Order Status Pages?

Thank You Page: This page appears after checkout and is often used for conversion tracking. Customers see it only once.
Order Status Page: This page allows customers to track and manage their orders. They can visit it multiple times through order notifications.

Before, merchants could customize these pages using Additional Scripts and apps with ScriptTags, but Shopify has removed this feature. Now, all merchants must switch to the new system, which supports app blocks and custom pixels.

How to Upgrade Your Pages

Shopify provides an upgrade guide to help you transition based on your current settings and installed apps. If your Thank You and Order Status pages do not require changes, you can simply press ‘Update’ to complete the migration. If you have custom scripts or modifications, you will need to adjust them manually.

Why This Update Matters

  • Improves page performance and security
  • Ensures compatibility with Shopify’s latest features
  • Supports better tracking and customization with app blocks

Need Help with Migration?

If you are unsure how to migrate your Thank You and Order Status pages, I can help:

✅ Migrate existing scripts to the new system

✅ Set up conversion tracking using app blocks and custom pixels

✅ Ensure a smooth transition without losing functionality

✅ Make design or content changes as needed

Act Now – Avoid Last-Minute Issues!

Shopify’s deadline is approaching, and delaying the migration could cause tracking errors or missing order updates. Don’t wait until the last minute!

Upgrade your Shopify store now and stay ahead of the changes!

How to Find a Shopify Store URL Without Domain Access

Introduction
When working on a Shopify store, sometimes clients forget to grant you access to the domain section. Without access, finding the store URL can be tricky. However, there’s an easy way to retrieve it directly from the Shopify admin panel. Follow these steps:

 

Step 1: Log in to the Client’s Store
Access the client’s Shopify admin panel using the provided login credentials.

 

Step 2: Check the URL Structure
Once logged in, navigate to any section within the Shopify admin, such as the theme settings. The URL should look something like this:

https://admin.shopify.com/store/samamatahir/themes

 

Step 3: Extract the Store Name
From the URL, locate the text between /store/ and /themes. In this example:

Store Name: samamatahir

 

Step 4: Construct the Store URL
Now, use the extracted store name to form the Shopify backend login link:

https://samamatahir.myshopify.com/admin

 

Final Step: Use the URL to Access the Store
Enter the generated URL into your browser, and you should be able to access the Shopify admin panel.

 

Conclusion
This method is useful when clients do not provide domain access. By following these steps, you can quickly retrieve the store URL and continue working without delays. Always ensure you have the necessary permissions before making any store changes.