How to set default product in cart

Sometimes you would like to have a default product show up in your customer’s cart. For example, that is a freegift product or just a product you would like to sell.
For doing that, you can use my small script bellow. Remember to put it before the closed body tag and change the ID of variant product to valid ID. You can choose auto reloading page after product has been added or redirecting it to specified page (cart page) if you want.

<script> Shopify = Shopify || {};
    Shopify.cart = {{ cart | json }};
    if(Shopify.cart !=='undefined'
      && Shopify.cart.items.length === 0){
      jQuery.post('/cart/add.js', {
        quantity: 1,
        id: 1152084600,	//put your varian id here (should valid id, if not your site will be reload with nonstop loop)
      }).always(function(data){
        //window.location.reload(); //uncomment if you want to reload page
        window.location.href="cart?backurl="+window.location.href;  //if you want redirect to cart page
      });
    }
  </script> 
</body>

Leave a Reply

Your email address will not be published. Required fields are marked *