Product SDK Methods

Overview

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

Methods

calculateProductVisibilityFromTags(productTags: string[]): Promise<boolean>

Determines if a product should be visible in a TryNow context based on the product's tags.

Parameters

ParameterTypeDescription
productTagsstring[]Array of product tags to check visibility for

Returns

Promise<boolean> - A promise that resolves to a boolean indicating if the product should be visible in a TryNow context.

Example

// Check if a product with specific tags should be visible
const productTags = ['try-now-eligible', 'apparel', 'new-arrival'];
window.trynow.product.calculateProductVisibilityFromTags(productTags)
  .then(isVisible => {
    if (isVisible) {
      console.log('Product is eligible for TryNow');
    } else {
      console.log('Product is not eligible for TryNow');
    }
  })
  .catch(error => {
    console.error('Error checking product visibility:', error);
  });

recalculateCtaButtonState(productData: ShopifyJsProduct): Promise<void>

Updates the product data within the app and recalculates the state of TryNow CTA buttons.

Deprecated: Use window.trynow.components.tryNowCtaButton.setProduct instead.

Parameters

ParameterTypeDescription
productDataShopifyJsProductThe product data to be used by the app, formatted according to Shopify's product schema

Returns

Promise<void> - A promise that resolves when the product data has been updated.

Example

// Update product data for TryNow buttons
const productData = {
  id: '1234567890',
  title: 'Sample Product',
  tags: ['try-now-eligible'],
  variants: [
    {
      id: '9876543210',
      price: '49.99',
      available: true
    }
  ]
  // Other Shopify product fields
};

// Deprecated approach
window.trynow.product.recalculateCtaButtonState(productData)
  .then(() => {
    console.log('CTA button state updated');
  })
  .catch(error => {
    console.error('Error updating CTA button state:', error);
  });

// Recommended approach
window.trynow.components.tryNowCtaButton.setProduct(productData)
  .then(() => {
    console.log('CTA button state updated');
  })
  .catch(error => {
    console.error('Error updating CTA button state:', error);
  });

Usage Notes

  • The ShopifyJsProduct interface follows Shopify's product schema. For more information, see Shopify's documentation.
  • When working with product visibility, consider using the calculateProductVisibilityFromTags method to determine if TryNow functionality should be available for a specific product.
  • For updating product data, prefer using the recommended window.trynow.components.tryNowCtaButton.setProduct method instead of the deprecated recalculateCtaButtonState.