Methods
All methods below are as if the cart object has a reference variable defined as shown in this code snippet.
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.
allItemsAreOneTime¶
Returns a boolean. If all items in the cart are onetime items as opposed to subscription items.
allItemsAreSubscription¶
Returns a boolean. If all items in the cart are subscription items as opposed to onetime items.
allSubscribableItemsAreSubscription¶
Returns a boolean. If all items in the cart that have a subscription option are subscription products.
availableSubscriptionOptions¶
Returns an options property which is an array containing all of the available frequency options of the items in the cart.
cartHasMetafieldSubscriptionItems¶
Returns a boolean. This would be in reference to stores using subscription items with Recharge Checkout which would return true.
cartHasSellingPlanSubscriptionItems¶
Returns a boolean. This would be in reference to Shopify selling plans or Recharges Shopify checkout integration which would return true.
cartHasSubscriptionItems¶
Returns a boolean if any of the items in the cart are a subscription item.
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.
// 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
clearCart¶
When this method is called all the items in the cart will be removed.
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.
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.
Takes an email address and passes the Rebuy customer object into the callback.
fetchReChargeCart¶
Passes the Recharge cart object into the callback
fetchShopifyCart¶
Async function that returns the Rebuy cart object.
formatMoney¶
This function takes a number as the first argument and returns a formatted string with decimal and dollar symbol.
getCart¶
Returns the Rebuy cart object.
getCartProductIDs¶
Returns tan array of numbers which are the product ID's of the products in the cart.
getCartToken¶
Returns a string which is the current Shopify cart token.
getCartVariantIDs¶
Returns tan array of numbers which are the variant ID's of the products in the cart.
getCurrency¶
Returns a string which is the current currency the store is using.
getItemAtIndex¶
Returns an item object at the index given.
getItemLineNumber¶
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.
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.
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.
getReChargeCheckoutDomain¶
Returns the Recharge checkout domain.
Recharge Checkout Only
Only use this if the merchant is using the Recharge checkout version of Recharge as opposed to the Shopify Checkout Integration.
goToCartPage¶
When this method is called the user will be redirected to the cart page.
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.
Recharge Checkout Only
Only use this if the merchant is using the Recharge checkout version of Recharge as opposed to the Shopify Checkout Integration.
goToShopifyCheckout¶
When this function is called, the customer will be re-directed to the Shopify checkout page.
hasItems¶
Returns a boolean (true) if the cart currently has any items.
hasSubscribableItems¶
Returns a boolean (true) when the cart contains at least one item that could be or is a subscription item.
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.
init¶
Initializes the Rebuy Cart object
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.
itemCount¶
Returns the count (type: number) of all the items including items with more than one quantity.
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')
itemDeliveryInterval¶
Returns a string which is the currently selected interval ('days').
itemIsOneTime¶
Returns a boolean (true) if the specified item is a onetime item as opposed to a subscription item.
itemIsSubscription¶
Returns a boolean (true) if the specified item is a subscription item as opposed to a onetime 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.
items¶
Returns an array of objects which are the items in the cart.
lineCount¶
Returns a number. If one item has quantity of two then that will only count as one toward to total count returned.
lineItemDiscountMessage¶
Returns a string which is the discount message if applicable to the item.
removeItem¶
Removes the item that is passed in as the first argument.
removeShopifyDiscount¶
selectVariant¶
setDiscount¶
setItemQuantity¶
Takes and item object and a number as arguments and will set that item quantity to the given number.
subtotal¶
Returns a number which is the current subtotal of all the items in the cart. This is not formatted.
switchCartToFrequency¶
switchItemToFrequency¶
switchItemToOneTimeData¶
switchItemToSubscriptionData¶
switchToOneTime¶
Takes a subscription item's object as an argument and will switch the item from a subscription to a one-time item.
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')
--- title: Methods excerpt: Below is a list of Rebuy's cart based methods. deprecated: false hidden: false metadata: title: '' description: '' robots: index next: description: '' --- All methods below are as if the cart object has a reference variable defined as shown in this code snippet. ```javascript title="Cart reference" 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. ```javascript Cart.addItem(data, options); ``` ## allItemsAreOneTime Returns a boolean. If all items in the cart are onetime items as opposed to subscription items. ```javascript Cart.allItemsAreOneTime(); ``` ## allItemsAreSubscription Returns a boolean. If all items in the cart are subscription items as opposed to onetime items. ```javascript Cart.allItemsAreSubscription(); ``` ## allSubscribableItemsAreSubscription Returns a boolean. If all items in the cart that have a subscription option are subscription products. ```javascript Cart.allSubscribableItemsAreSubscription(); ``` ## availableSubscriptionOptions Returns an options property which is an array containing all of the available frequency options of the items in the cart. ```javascript Cart.availableSubscriptionOptions(); ``` ## cartHasMetafieldSubscriptionItems Returns a boolean. This would be in reference to stores using subscription items with Recharge Checkout which would return true. ```javascript Cart.cartHasMetafieldSubscriptionItems(); ``` ## cartHasSellingPlanSubscriptionItems Returns a boolean. This would be in reference to Shopify selling plans or Recharges Shopify checkout integration which would return true. ```javascript Cart.cartHasSellingPlanSubscriptionItems(); ``` ## cartHasSubscriptionItems Returns a boolean if any of the items in the cart are a subscription item. ```javascript 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. ```javascript title="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 ```javascript Cart.checkout(); ``` ## clearCart When this method is called all the items in the cart will be removed. ```javascript 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. ```javascript 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. ```javascript 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 ```javascript Cart.fetchReChargeCart(callback); ``` ```javascript title="Example" Cart.fetchReChargeCart(cart => console.log("cart", cart)); ``` ## fetchShopifyCart Async function that returns the Rebuy cart object. ```javascript Cart.fetchShopifyCart(callback); ``` ```javascript title="Example" 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. ```javascript Cart.formatMoney(price, format); ``` ```javascript title="Example" Cart.formatMoney(10000); // Will return '$100.00' ``` ## getCart Returns the Rebuy cart object. ```javascript Cart.getCart(); ``` ## getCartProductIDs Returns tan array of numbers which are the product ID's of the products in the cart. ```javascript Cart.getCartProductIDs(); ``` ## getCartToken Returns a string which is the current Shopify cart token. ```javascript Cart.getCartToken(); ``` ## getCartVariantIDs Returns tan array of numbers which are the variant ID's of the products in the cart. ```javascript Cart.getCartVariantIDs(); ``` ## getCurrency Returns a string which is the current currency the store is using. ```javascript Cart.getCurrency(); ``` ## getItemAtIndex Returns an item object at the index given. ```javascript Cart.getItemAtIndex(index); ``` ## getItemLineNumber ```javascript 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. ```javascript 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. !!! note "Widget Tracking" Widget tracking must be enabled for this method to work properly. This is enabled on all widgets by default. ```javascript 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. !!! note "Widget Tracking" Widget tracking must be enabled for this method to work properly. This is enabled on all widgets by default. ```javascript Cart.getRebuyItems(widget_id); ``` ## getReChargeCheckoutDomain Returns the Recharge checkout domain. !!! warning "Recharge Checkout Only" Only use this if the merchant is using the Recharge checkout version of Recharge as opposed to the Shopify Checkout Integration. ```javascript Cart.getReChargeCheckoutDomain(); ``` ## goToCartPage When this method is called the user will be redirected to the cart page. ```javascript 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 "Recharge Checkout Only" Only use this if the merchant is using the Recharge checkout version of Recharge as opposed to the Shopify Checkout Integration. ```javascript Cart.goToReChargeCheckout(); ``` ## goToShopifyCheckout When this function is called, the customer will be re-directed to the Shopify checkout page. ```javascript Cart.goToShopifyCheckout(); ``` ## hasItems Returns a boolean (true) if the cart currently has any items. ```javascript Cart.hasItems(); ``` ## hasSubscribableItems Returns a boolean (true) when the cart contains at least one item that could be or is a subscription item. ```javascript 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. ```javascript Cart.increaseItem(item, success, error); ``` ```javascript title="Example" Cart.increaseItem(Cart.items[0]); ``` ## init Initializes the Rebuy Cart object ```javascript 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. ```javascript Cart.isSubscription(item); ``` ## itemCount Returns the count (type: number) of all the items including items with more than one quantity. ```javascript 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') ```javascript Cart.itemDeliveryFrequency(item); ``` ## itemDeliveryInterval Returns a string which is the currently selected interval ('days'). ```javascript Cart.itemDeliveryInterval(item); ``` ## itemIsOneTime Returns a boolean (true) if the specified item is a onetime item as opposed to a subscription item. ```javascript Cart.itemIsOneTime(item); ``` ## itemIsSubscription Returns a boolean (true) if the specified item is a subscription item as opposed to a onetime item. ```javascript 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. ```javascript Cart.itemPrice(item); ``` ## items Returns an array of objects which are the items in the cart. ```javascript 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. ```javascript Cart.lineCount(); ``` ## lineItemDiscountMessage Returns a string which is the discount message if applicable to the item. ```javascript Cart.lineItemDiscountMessage(item); ``` ## removeItem Removes the item that is passed in as the first argument. ```javascript Cart.removeItem(item, callback); ``` ## removeShopifyDiscount ```javascript Cart.removeShopifyDiscount(callback); ``` ## selectVariant ```javascript Cart.selectVariant(product, query); ``` ## setDiscount ```javascript Cart.setDiscount(discount); ``` ## setItemQuantity Takes and item object and a number as arguments and will set that item quantity to the given number. ```javascript 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. ```javascript Cart.subtotal(); ``` ## switchCartToFrequency ```javascript Cart.switchCartToFrequency(selectedOption, callback, errorHandler); ``` ## switchItemToFrequency ```javascript Cart.switchItemToFrequency(item, frequency, callback, errorHandler); ``` ## switchItemToOneTimeData ```javascript Cart.switchItemToOneTimeData(item); ``` ## switchItemToSubscriptionData ```javascript 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. ```javascript 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') ```javascript Cart.switchToSubscription(item, frequency, callback); ```