AJAX Add Product if cart > $50

Solved
Highlighted
Tourist
4 1 0

Hi there, I'm currently looking to have an AJAX call to add a free item (the free part is taken care of with automatic discounts) to customer's carts when their order value is >= $50.

Here is the code I have so far and am looking for help. I'm using the Supply theme and console shows that /cart/add.js is 404'ing.

 

  {% for line_item in cart.items %}

	{% if line_item.product_id == 5400398823577 %}

		{% assign foundBag = true %}

	{% endif %}

  {% endfor %}

	{% if cart.total_price >= 5000 and foundBag == false %}
    
    <script type="text/javascript">
      
		$(document).ready(function(){
          
          $.ajax({
            type: 'POST', 
            url: '/cart/add.js',
            dataType: 'json', 
            data: {
                quantity: 1,
                id: 5400398823577
            },
            success: alert("FREE Bag has been added to your cart!"),
            error: alert("Failed to add your free bag. Please contact support")
          });
          
        });
      
    </script>
    
  {% endif %}

 

 

0 Likes
Highlighted
Tourist
4 1 0

This is an accepted solution.

Solved my own issue. Was using the product ID instead of the variant ID while attempting to add the product.

Use this guide to find a product's variant ID.

0 Likes