Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

How to configure your Shopify Cart to redirect to a Checkout Shop page.

Info

Before to continue, you need to have your Shopify integration and your Shop or a Funnel with PowerCart activated in THIIO

...

2.- You need to edit your Shopify liquid code manually to redirect your cart page to the THIIO Checkout Shop Page.

...

3.- Go to the snippets/ folder.

...

4 .- Click in on Add a new snippet

...

5.- Create a new snippet thiio-cart.js

...

Code Block
{% unless checkout_url %}
  {% assign checkout_url = '[th-power-cart-page]' %}
{% endunless %}

{% unless checkout_button_selector %}
  {% assign checkout_button_selector = '[type="submit"][name="checkout"]' %}
{% endunless %}
<script>
  document.addEventListener("DOMContentLoaded", function () {
    var debug = true ? console.log.bind(console, '[DEBUG][RedirectCart]') : function () {};
    debug('Script loaded');

    window.RedirectCart = function (options) {
      var self = {};

      function init() {
        self.options = Object.assign({
          checkoutButtonSelector: '{{ checkout_button_selector }}',
          checkoutUrl: '{{ checkout_url }}',
        }, options);

        self.$checkoutButton = document.querySelector(self.options.checkoutButtonSelector)
        debug('Initialized with options', self.options);
        inject();
      }

      function inject() {
        debug('Inject');
        self.$checkoutButton.addEventListener('click', checkout);
      }

      async function checkout(event) {
        event.preventDefault();
        const checkoutUrl = await getCheckoutURL();
        window.location.href = checkoutUrl;
      }
      
      function getCartCookie(name) {
				const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
        if (match){ 
          return match[2];
        }
			}

      async function getCheckoutURL(products) {
        cookie = getCartCookie('cart');

        var cartContents = await fetch(window.Shopify.routes.root + 'cart.js')
        const cartData = await cartContents.json()

        const productCartIds = cartData.items.map(i => {
          return { product_id:i.product_id, variation_id: i.id, quantity:i.quantity}
        })

        return `${self.options.checkoutUrl}?sh_items=${btoa(JSON.stringify(productCartIds))}`;
      }

      init();

      return self;
    };

    var instance = new RedirectCart();

  });
</script>

7.- Save your the changes of your new snippet.

...