How to display the cart icon once an item is added Squarespace 7.1

This tutorial will show you how to only display the cart icon once an item has been added. The code used was provided by Purple Giraffe. It has been adpated for Squarespace 7.1

Complexity: Easy


Step 1

From your Squarespace account, simply add the following JavaScript below to Settings > Advanced > Code Injection > Footer and then save your changes.

<script>
 /**
  * Hide Cart until items in the cart
  */
 (function () {
   var carts = [].slice.call(document.querySelectorAll('.header-actions-action--cart'));
   carts.forEach(function(cart) {
     hideCart(cart);
   });
   /**
    * Hide Cart
    */
   function hideCart(cart) {
     var cartQty = cart.querySelector('.sqs-cart-quantity');
     // Handler
     function handler(target) {
       if (target.innerHTML === '0') {
         cart.setAttribute('hidden', '');
       } else {
         cart.removeAttribute('hidden');
       }
     }
     // Observer handler
     var observer = new MutationObserver(function(mutations) {
       handler(mutations[0].target);
     });
     // Hide/show the cart when the page is loaded
     handler(cartQty);
     // Hide/show the cart when an item was added/removed
     observer.observe(cartQty, { childList: true });
   }
 })();
 </script>
Cart hidden

Step 2

Item added to cart

Cart visible


All work in this guide is provided ?“AS IS”. Other than as provided in this agreement, this guide makes no other warranties, express or implied, and hereby disclaims all implied warranties, including any warranty of fitness for a particular purpose.

If you require professional advice, we recommend that you purchase the services of a developer.

Primitus Consultancy

We work with small and medium-sized businesses to help create a professional online presence. We provide a one shop full-service design studio in London, United Kingdom. 

https://primitusconsultancy.co.uk
Previous
Previous

How to add split navigation to Squarespace 7.1

Next
Next

How to create a flip effect on social media icons and button on Squarespace 7.1