Collection

An easy way to access and use Shopify Collection data within the Superfans SDK.

Description

The collection variable provides details about the collection on a product listing (PLP) page. It includes the collection’s ID, title, handle, description, and main image. The collection object is only available on PLP pages; outside of a PLP context, it is null.

Properties

PropertyTypeScopeDescriptionExample
collection.idStringPLPUnique id of the Collection123457
collection.titleStringPLPThe name of the collection provided in ShopifyVintage Shirts
collection.handleStringPLPUser-friendly slug used in Shopify collection URL.vintage-shirts
collection.descriptionStringPLPThe textual description provided for the collection in Shopify. (May include HTML content)Limited availability vintage shirts
collection.imageStringPLPThe image of the collection uploaded in Shopify.https://example.com/image.jpg

Example Usage


// Access the collection on a PLP page to customise the listing or to generate headings. 

const collection_details = VajroSDK.variables.collection;

if (collection_details) {
  // Display collection title and description
  document.getElementById('collection-title').innerText = collection_details.title;
  document.getElementById('collection-desc').innerText = collection_details.description;

  // Show collection image
  const img = document.getElementById('collection-image');
  img.src = collection_details.image;
} else {
  // Fallback for non-PLP pages
  console.log("No collection context available.");
}

Best Practices

  1. Check for null: collection is only defined on PLP pages. Outside of PLP, always handle null.
  2. Read‑only fields: Do not attempt to modify collection properties; they reflect Shopify data.
  3. Use the handle for navigation: When linking to a collection page, use collection.handle to generate URLs.

Caveats

  1. Context‑dependent: The collection variable is scoped to the PLP page and is unavailable in other contexts
  2. Optional image: Some collections may not have an image; verify collection.image exists before using it.
  3. HTML content: collection.description can contain HTML tags; escape or sanitize before injecting into the DOM to prevent XSS issues.