> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://regiostech.crisp.help/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# DOPP API: Examples

This guide provides practical examples for using the DOPP API to manage and customize discount flows on your store. Each example covers a real-world scenario, helping you understand how to implement features like overriding cart items, specifying product collections, and reloading discounts in dynamic situations such as variant changes or infinite scrolling. 

### Example Scenarios

1. **Override Cart Items for Bundle Discounts**
2. **Specify Product Collections for Collection-Based Discounts**
3. **Simulate Customer Tags for Loyalty Discounts**
4. **Trigger Discount Reload on Variant Change for Product Page Updates**
5. **Refresh Collection Page Discounts on New Items or Infinite Scroll**
---

## API Use Case Examples

### Override Cart Items for Bundle Discounts

**Scenario**: You want to calculate discounts based on a custom cart setup, such as a product bundle, to see how discounts apply before the items are actually in the cart.

```javascript
const overriddenCartLines = [
  { productId: 111, variantId: 222, regularPriceInCents: 1500, quantity: 2 },
  { productId: 333, variantId: 444, regularPriceInCents: 2500, quantity: 1 }
];

try {
  const result = await RegiosDOPP.api.v0?.calculateDiscountedPrices({
    productId: 123,
    variantId: 456,
    regularPriceInCents: 1000,
    cartLines: overriddenCartLines
  });
  console.log("Discounted Price with Override:", result);
} catch (error) {
  console.error("Error:", error);
}
```

### Specify Product Collections for Collection-Based Discounts

**Scenario**: If an existing discount is only applied when products belong to specific collections (like a discount limited to a "Clearance" collection), specify `collectionIds` to indicate the collections the product belongs to. This lets the API consider any collection-based discount conditions.

```javascript
const product = {
  productId: 123,
  variantId: 456,
  regularPriceInCents: 1000,
  collectionIds: [101, 202, 303] // IDs of relevant collections
};

try {
  const result = await RegiosDOPP.api.v0?.calculateDiscountedPrices(product);
  console.log("Discounted Price with Collections:", result);
} catch (error) {
  console.error("Error:", error);
}
```

### Simulate Customer Tags for Loyalty Discounts

**Scenario**: If you offer discounts to specific customer groups, such as VIPs or loyal customers, simulate those tags to test discount calculations without requiring the customer to log in.

```javascript
const simulatedCustomer = {
  id: 789,
  tags: ["VIP", "LoyalCustomer"]
};

try {
  const result = await RegiosDOPP.api.v0?.calculateDiscountedPrices({
    productId: 123,
    variantId: 456,
    regularPriceInCents: 1000,
    customer: simulatedCustomer
  });
  console.log("Discounted Price with Customer Tags:", result);
} catch (error) {
  console.error("Error:", error);
}
```

### Trigger Discount Reload on Variant Change for Product Page Updates

**Scenario**: If your theme has complex behavior around variant selection, trigger a discount recalculation by dispatching a `regios-dopp:variant-change` event. This ensures that discounts update correctly as variants change.

```javascript
window.dispatchEvent(new CustomEvent("regios-dopp:variant-change", {
  detail: {
    selectedVariant: {
      id: 123456789,  // New variant ID
      price: 3000     // Price of the new variant in cents
    },
    quantity: 1       // Quantity of the selected variant
  }
}));
```

### Refresh Collection Page Discounts on New Items or Infinite Scroll

**Scenario**: For collection pages with a “Load More” button or infinite scroll, use the `regios-dopp:collection-page:new-items` event to trigger discount recalculations as new items load. This keeps the discount data accurate for all visible products.

```javascript
function refreshDiscountsOnNewItems(newItems) {
  window.dispatchEvent(new CustomEvent("regios-dopp:collection-page:new-items", {
    // `uniqueKey` default is "default". Or you can omit this if you only have one "Collection Page Discount" block on the page.
    detail: { uniqueKey: 'default', newItems }
  }));
}

// Example usage: call `refreshDiscountsOnNewItems` when new items are fetched
await refreshDiscountsOnNewItems([
  { productId: 555, variantId: 666, regularPriceInCents: 2000 },
  { productId: 777, variantId: 888, regularPriceInCents: 3000 }
]);
```

This guide ensures you can effectively use the DOPP API for dynamic discount scenarios on your store.

## Your Feedback Matters
Your feedback helps us improve. Could you please take a moment to [leave a review on the Shopify App Store](https://apps.shopify.com/regios-automatic-discounts#modal-show=ReviewListingModal)? Thank you!