Redirect to subscription checkout
In some cases, you may want a button that bypasses the cart page and goes straight to the subscription checkout. This is useful if you are creating a buy button on a non-product page in Shopify or if you'd like to minimize the number of steps to purchase. This guide covers how to achieve this using JavaScript.
- Recharge Checkout on Shopify
- Shopify Checkout Integration
Before you start
- This feature will require advanced JavaScript knowledge. This is not part of Recharge's standard turnkey solution. See the Recharge Design Policy for additional information.
- The starter code offered in this article applies to Shopify's Theme files.
- Before using the JavaScript below, we recommend that you first use AJAX to add the item to the cart.
- The code below will work best if your store only sells subscription products and does not offer one-time products.
Redirect from product page
This is the JavaScript redirect to send the window location to the Recharge checkout page. Make sure the product is added to the cart first, as a cart token must exist for the script to work.
Warning: The function below should be called as part of a success call (if using AJAX), or added to an event listener, such as a button click.
function reChargeProcessCart() {
function get_cookie(name){ return( document.cookie.match('(^|; )'+name+'=([^;]*)')||0 )[2] }
do {
token=get_cookie('cart');
}
while(token == undefined); var myshopify_domain='{{ shop.permanent_domain }}'
try { var ga_linker = ga.getAll()[0].get('linkerParam') } catch(err) { var ga_linker ='' }
checkout_url= "https://checkout.rechargeapps.com/r/checkout?myshopify_domain="+myshopify_domain+"&cart_token="+token+"&"+ga_linker;
}
Redirect from cart page
An alternative is to still direct people to the cart page, and then on the cart page run an auto-redirect to trigger the checkout button immediately. You can do so by adding the code below to the cart.liquid file. Note that this will still cause the cart page to load between the product page and checkout.
<script>$(window).ready(function() {$('[name="checkout"]').click();});</script><br>