The widget methods are scoped to each widget specifically. The widgets are placed in an array nested within the global Rebuy object so when using methods for a specific widget you can use the Array.find() function (example below) or any other array function that you prefer.
This is not a complete list of the globally available function but the ones that we find most helpful.
const widget = window.Rebuy.widgets.find(widget => widget.id === "1234");
All methods below are as if there is a reference to a specific widget such as the snippet below or if the code snippet is in one of the widget callbacks from the widget settings page under the Advanced settings such as the image below.
const widget = window.Rebuy.widgets[0];
addSelectedProductsToCart
This function is used with the Product Add-Ons widget. This adds the items that are selected from the widget to the cart.
widget.addSelectedProductsToCart(data, callback);
addToCart
Makes a post request to Shopify API to add an item to the cart. The first argument expects a product object.
widget.addToCart(product, callback);
addToReChargeCheckout
Used by the Recharge Checkout widget to add the item object passed in as the first argument and will update the checkout UI.
widget.addToReChargeCheckout(product, callback);
cartItemIsWidgetItem
Returns a boolean (true) if the item passed in as an argument has been added by the specified widget that is calling the method.
widget.cartItemIsWidgetItem(item);
checkout
When this function is called, the customer will be redirected to the checkout page.
widget.checkout();
compareAtSubtotal
Used by the Dynamic Bundle and Product Addons widgets to generate a compare at price if there is a discount for any of the applicable products
widget.compareAtSubtotal();
declineOffer
Used by the Upsell, Thanks You Page and Switch to Subscription popup type widgets when the customer does not want to accept the offer from presented.
widget.declineOffer(product, callback);
destroy
This function will destroy and remove the widget, including the root div, at the given index. This function calls hide, detachWidget and unbindEvents.
widget.destroy(callback);
detachWidget
This function removes the widget from the DOM as well as removes it from the array of widget from window.Rebuy.widgets.
widget.detachWidget();
getCartProductIDs
Returns an array of numbers which are the product ID's of the products in the cart.
widget.getCartProductIDs();
getCartVariantIDs
Returns an array of numbers which are the variant ID's of the products in the cart.
widget.getCartVariantIDs();
getVariantIDs
Returns an array of numbers which are the variant ID's of the items in the cart. Take an optional boolean (join) as an argument and when true will join as a comma separated string instead of an array of numbers.
widget.getVariantIDs(join);
getWidgetProducts
This method will force the widget to fetch new products via the API, and accepts an optional callback function. This is useful if you'd like to update product or variant IDs associated with the widget and then fetch a new API response. In practice, you could update the variant ID based on a customer's interaction with the product page form and fetch variant-specific recommendations.
widget.getWidgetProducts(callback):
// Set Shopify variant IDs
widget.data.shopify_variant_ids = [1234567890];
// Fetch new data with the updated variant IDs
widget.getWidgetProducts(()=>{
console.log("New products fetched!");
});
hasQuantityInputEnabled
Returns a boolean (true) when the widgets quantity input setting is enabled.
widget.hasQuantityInputEnabled();
hasTimer
Returns a boolean (true) when the widgets timer setting is enabled.
widget.hasTimer();
hide
Used by Popup type widgets and will close the widget when this method is called.
widget.hide();
isCartBasedWidget
Returns a boolean (true) when the widget is a cart based widget as opposed to a product page widget.
widget.isCartBasedWidget();
productIsSelected
Used by the Dynamic Bundle and Product Addons widgets. Returns a boolean (true) when the checkbox is selected.
widget.productIsSelected(product);
removeFromCart
Original use case: Used by the Shopify checkout and Recharge checkout widget to remove an item from the cart that has been added by the widget. When in checkout, if an item is added by a Rebuy checkout widget then the item will have a (remove) button below it. This way, if the item was added accidentally the customer does not need to navigate back to the cart page to adjust the item that was added.
widget.removeFromCart(product, callback);
render
When this method is called and the widget should display based off the connected rules then it will render by creating a new Vue instance for the widget.
widget.render();
selectedProductCount
Used by the Product Addon and Dynamic bundle widget. Returns a number which is the amount of products that are currently selected.
widget.selectedProductCount();
shouldDisplay
Returns a boolean (true) if the widget should display according to the connected data source.
widget.shouldDisplay();
show
This method will make a popup type widget display when it is called. The popup widget can be set to trigger manually via the widgets settings object and then could be triggered to display according to custom logic as needed.
widget.show();
widget.data.pop_up_trigger = "manual";
if ("some custom logic") {
widget.show();
}