logo

Select Sidearea

Populate the sidearea with useful widgets. It’s simple to add images, categories, latest post, social media icon links, tag clouds, and more.
hello@youremail.com
+1234567890
 

How to set default product in cart

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>
No Comments

Post a Comment

Comment
Name
Email
Website