Methods

Below is a list of Rebuy's cart based methods.

All methods below are as if the cart object has a reference variable defined as shown in this code snippet.

const Cart = window.Rebuy.Cart;

This is not a complete list of the globally available function but the ones that we find most helpful.

addItem

Adds the item object that is passed in as the first argument to the Shopify cart.

Cart.addItem(data, options);

allItemsAreOneTime

Returns a boolean. If all items in the cart are onetime items as opposed to subscription items.

Cart.allItemsAreOneTime();

allItemsAreSubscription

Returns a boolean. If all items in the cart are subscription items as opposed to onetime items.

Cart.allItemsAreSubscription();

allSubscribableItemsAreSubscription

Returns a boolean. If all items in the cart that have a subscription option are subscription products.

Cart.allSubscribableItemsAreSubscription();

availableSubscriptionOptions

Returns an options property which is an array containing all of the available frequency options of the items in the cart.

Cart.availableSubscriptionOptions();

cartHasMetafieldSubscriptionItems

Returns a boolean. This would be in reference to stores using subscription items with Recharge Checkout which would return true.

Cart.cartHasMetafieldSubscriptionItems();

cartHasSellingPlanSubscriptionItems

Returns a boolean. This would be in reference to Shopify selling plans or Recharges Shopify checkout integration which would return true.

Cart.cartHasSellingPlanSubscriptionItems();

cartHasSubscriptionItems

Returns a boolean if any of the items in the cart are a subscription item.

Cart.cartHasSubscriptionItems();

changeItem

Accepts an object which must contain an id (this should be the key of the item that you would like to change) property and a quantity property but could also adjust product properties. The options argument is also an object which can take a success and/or error property which are callback functions upon success or error.

Cart.changeItem(object, options);

// Example API call
Cart.changeItem({
  id: item.key,
  quantity: 2,
  properties: {
    test: 'This is a test'
  }
});

checkout

Will navigate the user to Shopify checkout if all items are onetime or if the merchant is set up with Shopify selling plans and subscription items are in the cart. Otherwise this function will navigate the user to Recharge checkout

Cart.checkout();

clearCart

When this method is called all the items in the cart will be removed.

Cart.clearCart(options);

decreaseItem

This function takes in a single item object as it first argument and an optional callback. One way to get the item object is using the window.Rebuy.Cart.getItemAtIndex(1); method if the index is known.

Cart.decreaseItem(item, callback);

enrichCart

This method loops through the cart items and appends additional information to the item by adding a product property. Within this product property you will find useful information referencing the map between the one-time product and the subscription version of that product depending on which checkout version of Recharge the merchant is using. This method also stores this information in Local Storage to be more easily accessed again in the future.

Cart.enrichCart(callback);

Takes an email address and passes the Rebuy customer object into the callback.

fetchReChargeCart

Passes the Recharge cart object into the callback

Cart.fetchReChargeCart(callback);
Cart.fetchReChargeCart(cart => console.log("cart", cart));

fetchShopifyCart

Async function that returns the Rebuy cart object.

Cart.fetchShopifyCart(callback);
Cart.fetchShopifyCart(cart => console.log("cart", cart));

formatMoney

This function takes a number as the first argument and returns a formatted string with decimal and dollar symbol.

Cart.formatMoney(price, format);
Cart.formatMoney(10000);
// Will return '$100.00'

getCart

Returns the Rebuy cart object.

Cart.getCart();

getCartProductIDs

Returns tan array of numbers which are the product ID's of the products in the cart.

Cart.getCartProductIDs();

getCartToken

Returns a string which is the current Shopify cart token.

Cart.getCartToken();

getCartVariantIDs

Returns tan array of numbers which are the variant ID's of the products in the cart.

Cart.getCartVariantIDs();

getCurrency

Returns a string which is the current currency the store is using.

Cart.getCurrency();

getItemAtIndex

Returns an item object at the index given.

Cart.getItemAtIndex(index);

getItemLineNumber

Cart.getItemLineNumber(item);

getItems

Returns an array containing the objects of the items currently in the cart in sorted order where index 0 is the most recent item added or updated.

Cart.getItems(filters, possessive);

getRebuyItemCount

Returns a number which is the number of items added by a Rebuy widget. If a specific widget ID is passed into this method as an argument then it will return the number of items that have been added by that widget only.

📘

Widget Tracking

Widget tracking must be enabled for this method to work properly. This is enabled on all widgets by default.

Cart.getRebuyItemCount(widget_id);

getRebuyItems

Returns an array of objects which are the items added by a Rebuy widget. If a specific widget ID is passed into this method as an argument then it will return the items that have been added by that widget only.

📘

Widget Tracking

Widget tracking must be enabled for this method to work properly. This is enabled on all widgets by default.

Cart.getRebuyItems(widget_id);

getReChargeCheckoutDomain

Returns the Recharge checkout domain.

🚧

Warning

Only use this if the merchant is using the Recharge checkout version of Recharge as opposed to the Shopify Checkout Integration.

Cart.getReChargeCheckoutDomain();

goToCartPage

When this method is called the user will be redirected to the cart page.

Cart.goToCartPage();

goToReChargeCheckout

When this method is called and the store is using the Recharge Checkout Integration the user will be redirected to the Recharge checkout page.

🚧

Warning

Only use this if the merchant is using the Recharge checkout version of Recharge as opposed to the Shopify Checkout Integration.

Cart.goToReChargeCheckout();

goToShopifyCheckout

When this function is called, the customer will be re-directed to the Shopify checkout page.

Cart.goToShopifyCheckout();

hasItems

Returns a boolean (true) if the cart currently has any items.

Cart.hasItems();

hasSubscribableItems

Returns a boolean (true) when the cart contains at least one item that could be or is a subscription item.

Cart.hasSubscribableItems();

increaseItem

Takes an item object as the first argument, a success callback as the second, and an error callback as the last and will increase the quantity of that item by one.

Cart.increaseItem(item, success, error);
Cart.increaseItem(Cart.items[0]);

init

Initializes the Rebuy Cart object

Cart.init();

isSubscription

Returns a boolean (true) if the product in the cart either is a subscription item or has a subscription option and is currently a onetime item.

Cart.isSubscription(item);

itemCount

Returns the count (type: number) of all the items including items with more than one quantity.

Cart.itemCount();

itemDeliveryFrequency

Takes an item object as an argument and returns a string which is the delivery frequency at which the item is currently selected at. (i.e. 'Onetime', '30', '60')

Cart.itemDeliveryFrequency(item);

itemDeliveryInterval

Returns a string which is the currently selected interval ('days').

Cart.itemDeliveryInterval(item);

itemIsOneTime

Returns a boolean (true) if the specified item is a onetime item as opposed to a subscription item.

Cart.itemIsOneTime(item);

itemIsSubscription

Returns a boolean (true) if the specified item is a subscription item as opposed to a onetime item.

Cart.itemIsSubscription(item);

itemPrice

Returns an unformatted number of the price of the item. Could be used in conjunction with Cart.formatMoney() to create a formatted price.

Cart.itemPrice(item);

items

Returns an array of objects which are the items in the cart.

Cart.items();

lineCount

Returns a number. If one item has quantity of two then that will only count as one toward to total count returned.

Cart.lineCount();

lineItemDiscountMessage

Returns a string which is the discount message if applicable to the item.

Cart.lineItemDiscountMessage(item);

removeItem

Removes the item that is passed in as the first argument.

Cart.removeItem(item, callback);

removeShopifyDiscount

Cart.removeShopifyDiscount(callback);

selectVariant

Cart.selectVariant(product, query);

setDiscount

Cart.setDiscount(discount);

setItemQuantity

Takes and item object and a number as arguments and will set that item quantity to the given number.

Cart.setItemQuantity(item, quantity, callback);

subtotal

Returns a number which is the current subtotal of all the items in the cart. This is not formatted.

Cart.subtotal();

switchCartToFrequency

Cart.switchCartToFrequency(selectedOption, callback, errorHandler);

switchItemToFrequency

Cart.switchItemToFrequency(item, frequency, callback, errorHandler);

switchItemToOneTimeData

Cart.switchItemToOneTimeData(item);

switchItemToSubscriptionData

Cart.switchItemToSubscriptionData(item, frequency, interval);

switchToOneTime

Takes a subscription item's object as an argument and will switch the item from a subscription to a one-time item.

Cart.switchToOneTime(item, callback);

switchToSubscription

Takes an item object and a frequency string. The frequency is the interval at which you would like the shipping interval to default to. (i.e. '30')

Cart.switchToSubscription(item, frequency, callback);