Cart SDK Methods

Overview

CartMethods is a singleton object exposed through the TryNow SDK that provides methods for cart-related interactions with the TryNow Shopify Integration. It's accessible via window.trynow.cart.

Properties

PropertyTypeDescription
observerCartObserverReference to the CartObserver instance that monitors cart changes

Methods

isTryNowCartLimitMet(): Promise<boolean>

Determines if the TryNow cart limit has been reached.

Returns

Promise<boolean> - A promise that resolves to a boolean indicating if the cart limit has been met.

Example

window.trynow.cart.isTryNowCartLimitMet()
  .then(isLimitMet => {
    if (isLimitMet) {
      console.log('Cart limit has been reached');
      // Disable adding more TryNow items
    } else {
      console.log('Cart limit has not been reached');
      // Allow adding more TryNow items
    }
  })
  .catch(error => {
    console.error('Error checking cart limit:', error);
  });

getAmountDueToday(): Promise<string>

Calculates the amount due today (excluding TryNow items) and returns it as a formatted string.

Returns

Promise<string> - A promise that resolves to a formatted string representing the amount due today.

Example

window.trynow.cart.getAmountDueToday()
  .then(amountDue => {
    console.log(`Amount due today: ${amountDue}`);
    // Display the amount due today to the user
  })
  .catch(error => {
    console.error('Error getting amount due today:', error);
  });

getTryNowItemCount(): Promise<number>

Gets the total quantity of TryNow items in the cart.

Returns

Promise<number> - A promise that resolves to the number of TryNow items in the cart.

Example

window.trynow.cart.getTryNowItemCount()
  .then(count => {
    console.log(`Number of TryNow items in cart: ${count}`);
    // Use the count to update UI or make decisions
  })
  .catch(error => {
    console.error('Error getting TryNow item count:', error);
  });

getIsCurrencyEligible(): Promise<boolean>

Checks if the cart's currency is eligible for TryNow.

Returns

Promise<boolean> - A promise that resolves to a boolean indicating if the cart's currency is eligible for TryNow.

Example

window.trynow.cart.getIsCurrencyEligible()
  .then(isEligible => {
    if (isEligible) {
      console.log('Cart currency is eligible for TryNow');
      // Enable TryNow functionality
    } else {
      console.log('Cart currency is not eligible for TryNow');
      // Disable TryNow functionality
    }
  })
  .catch(error => {
    console.error('Error checking currency eligibility:', error);
  });

getCartLimitUpperBound(): Promise<number>

Gets the maximum number of TryNow items allowed in the cart.

Returns

Promise<number> - A promise that resolves to the upper bound of the cart limit.

Example

window.trynow.cart.getCartLimitUpperBound()
  .then(limit => {
    console.log(`Maximum TryNow items allowed: ${limit}`);
    // Use the limit to update UI or make decisions
  })
  .catch(error => {
    console.error('Error getting cart limit upper bound:', error);
  });

getSuggestedTryNowCartSize(): Promise<number>

Gets the suggested number of TryNow items for the cart.

Returns

Promise<number> - A promise that resolves to the suggested TryNow cart size.

Example

window.trynow.cart.getSuggestedTryNowCartSize()
  .then(suggestedSize => {
    console.log(`Suggested TryNow cart size: ${suggestedSize}`);
    // Use the suggested size to guide the user
  })
  .catch(error => {
    console.error('Error getting suggested cart size:', error);
  });

replaceElementContentWithAmountDue(element: HTMLElement): Promise<void>

Updates the content of an HTML element with the formatted amount due today.

Parameters

ParameterTypeDescription
elementHTMLElementThe HTML element whose content will be replaced with the amount due today

Returns

Promise<void> - A promise that resolves when the element's content has been updated.

Example

// Get an element to update with the amount due today
const amountDueElement = document.getElementById('amount-due-today');

// Update the element with the amount due today
window.trynow.cart.replaceElementContentWithAmountDue(amountDueElement)
  .then(() => {
    console.log('Amount due today has been updated in the UI');
  })
  .catch(error => {
    console.error('Error updating amount due today in UI:', error);
  });

Usage Notes

  • All methods return promises and should be handled with appropriate error catching.
  • The cart methods rely on the current state of the cart, which is managed by the CartObserver.
  • The currency eligibility check uses the constant ELIGIBLE_CURRENCY to determine if the cart's currency is supported.
  • The amount due today is calculated by subtracting the price of TryNow items from the total cart price.
  • The cart limit and suggested cart size are retrieved from the platform data service.